I have some codes below, the choice
takes only integer input but prints out something special if the input is not an integer. However, the codes below to treat this issue seems a little bit lengthy. Anyway to fix it?
from sys import exit
def gold_room():
print "This room is full of gold. How much do you take?"
choice = raw_input("> ")
if "0" in choice or "1" in choice or "2" in choice or "3" in choice or "4" in choice or "5" in choice or "6" in choice or "7" in choice or "8" in choice or "9" in choice:
how_much = int(choice)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(1)
else:
dead("You're greedy!")
def dead(why):
print why, "Good job!"
exit(0)
gold_room()
Try something like:
try:
how_much = int(choice)
except ValueError:
dead('Man, learn to type a number.')
and look up Easier to ask for forgiveness than permission for the rationale.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments