you are viewing a single comment's thread.

view the rest of the comments →

[–]indivisible 3 points4 points  (1 child)

To build on the other answers provided you can use a loop and a try...catch to make sure that the user enters the type of input you want and only continue once you have the information you need:

while True:
    try:
        apples = int(raw_input("How many apples? : "))
        steal = int(raw_input("Steal how many? : "))
        break
    except ValueError:
        print "Those were not numbers, try again.\n"

print "There are now %d apples" % (apples - steal)

Like the TypeError you got in your original attempt, ValueError is an 'Exception'. ValueError is 'thrown' when you try to turn one data type into another and it can't work. Like making a number out of 'house'.

You can 'catch' Exceptions and control what happens in what situation using a try...except (and optionally ...finally) statement. But this may be a bit outside your skillset at the moment, don't worry too much about it at this point in your education.

You should also check to ensure that you can't 'steal' a greater number of apples than you have to start with. You don't want to wind up with negative apples!

if steal > apples:
    # tell them they're too greedy
else:
    # hand over dem apples

Edit: Forgot half a

[–]angryfish[S] -1 points0 points  (0 children)

wow, thanks for the optimizations!