Begginers Guide - Exrcise. Help me, plz!

Jeff Shannon jeff at ccvcorp.com
Mon Mar 18 15:40:38 EST 2002


Jeffrey-man wrote:

> Thank you very much!
> Here it is the final version:

This is good, but there's a few improvements that could be
made.  (I've reindented your code -- single-space indents
are hard to read.)


> sum = 0
> sum_stop = 100
> while sum < 100:

You've defined sum_stop, but you're not using it.


>     num = input("Please enter the number: ")
>     sum = sum+num
>     if sum < 100:
>         print "The sum is", sum
>     else:
>         print "The sum is > 100 and it is", sum

As long as you're in your while loop, you *know* that sum <
100, so doing this explicit 'if' test isn't really
necessary.

Try something like this:

while sum < sum_stop:
    num = input("Please enter the number:")
    sum = sum + num    # could also be  sum += num
    print "The sum is", sum
print "and it is > 100"

This will print "The sum is ..." after every number input.
When the sum goes over 100, the while loop ends and it
*also* prints the "and it is..." line.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list