I love python so I thought I would add a few points..
* You never have to set add = 0 since you dont do any checks against it before setting it later in the code
* You shouldnt add and then remove from total if it was to high. Try to only set a value if it will come into the range you want it to be after the setting.
* It is possible to use \n in strings to add newlines. This is more of a style thing, many people prefer the explicit prints
Here is a quick piece of code of how I would write it.
Of course I dont have all your prints in this simplified version but it shows how to bring together the logic.
- Code: Select all
#! /bin/env python
total = 0
print "\nLets play a game!"
while total < 100:
try:
add = int(raw_input("\nTotal is %s, add another number: " % total))
if total+add <= 100: total += add
else: print "\nTotal must not be greater than 100"
except ValueError: pass
print "\nYou won! Total is %s" % total
-- Wed Aug 05, 2009 1:22 am --
Also it might be worth noting that I totally forgot about negative integers. You can pass -1000 to this program.
- Code: Select all
if 0 < total+add <= 100: total += add # This should fix it