Begginers Guide - Exrcise. Help me, plz!

Jeff Shannon jeff at ccvcorp.com
Fri Mar 22 13:38:22 EST 2002


Jim Dennis wrote:

>  Shouldn't all these (helpful) examples be using raw_input() rather
>  than input().  Do we really want the new user to be implicitly and
>  blindly using eval(raw_input())?

Yes, they should.  :)  The calls to input() should, in this case, all be
replaced by int(raw_input()), and optionally wrapped in a try/except and while
loop, so that the program can prompt again if a non-numeric value is given.  I
almost mentioned this in my earlier response, but decided not to overly confuse
the O.P. -- this essentially doubles the code size.

while sum < sum_stop:
    num = None
    while num is None:
        try:
            num = int(raw_input("Please enter the number:"))
        except ValueError:
            print "That's not a number.  Try again!"
    sum = sum + num    # could also be  sum += num
    print "The sum is", sum
print "and it is > 100"

Alternatively, instead of setting num to None and watching for that to change,
we could use a 'while 1:  try: ...  except: ... else: break'  structure.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list