New to Python

Stargaming stargaming at gmail.com
Mon Mar 5 13:41:52 EST 2007


dnwurtz at gmail.com schrieb:
> I am trying to get a program to add up input from the user to get to
> the number 100 using a loop.  However, I am having some issues.  Here
> is what I have so far.  I know I am just trying to hard, but I am
> stuck.  Thank you for any help.
> 
> print "We need to count to 100"
> 
> high_number = 100
> total = 0
> 
> number = input("Enter your first number ")
> sum = number + total
> while sum < high_number:
>     print "Not there yet..."
>     number = input("Enter another number ")
> 
> print "We are there!!!"
> 

Just reading the error messages would be a good start. You assign `sum` 
to `number + total` but as I see it there's no `number` so far. You have 
to increment some variable with the user's input. (`sum += input(..)`)
Additionally, `total` and `number` seem pretty redundant. You only need 
two variables, one containing the targeted number, one the current progress.
By the way, input() is considered pretty evil because the user can enter 
arbitrary expressions. It is recommended to use raw_input() (till Python 
3.0), in your case e. g. int(raw_input()) wrapped in a try-except block.
HTH,
Stargaming



More information about the Python-list mailing list