Hi! I made a few changes to the program so that it can handle both int data types and strings, and so basically simplify it a bit. Here is the program (only the part that I made):
ef gold_room():
print("This room is full of gold. How much do you take?")
choice = input('> ')
try:
how_much = int(choice)
if how_much <= 100:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greedy bastard!")
except ValueError:
if 'all' in choice:
dead("You greedy bastard!")
else:
dead("Man, learn to type a number")
Is this a good way to do this? What are some ways in which I can improve it? Thanks!
Small typo: Iâm pretty sure that first line should be:
def gold_room():
other than that, sure, thatâs a reasonable way of input-checking.
I was curious about the use of ValueError, and played with it some.
I notice that if you remove ValueError, the code will go into the âexcept:â block even if you enter a valid number. But since you did enter a number, it will also do the test for âall.â So if you enter a valid number that is below the âgreedy bastardâ cutoff, you get the odd outcome of first winning (âNice, youâre not greedy!â) but then dying (âMan, learn to type a number!â death) because the except: block has no entry condition attached. Assuming I have all that straightâŚ
This could also probably be done with elif statements. Going to try that just for giggles, but itâll probably end up complicated and clunky.
@florian Oh⌠Yeah youâre right, I canât just kill folk if they arenât greedy⌠I added just one elif to protect people who are honest:
def gold_room():
print("This room is full of gold. How much do you take?")
choice = input('> ')
try:
how_much = int(choice)
if how_much <= 100:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greedy bastard!")
except ValueError:
if 'all' in choice:
dead("You greedy bastard!")
elif 'none' in choice or 'zero' in choice:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("Man, learn to type a number")
@florian Okay, how about just warning the player like here (In the first print line):
def gold_room():
print("This room is full of gold. How much do you take? (Enter a number or you're dead.)")
choice = input('> ')
try:
how_much = int(choice)
if how_much <= 100:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greedy bastard!")
except ValueError:
if 'all' in choice:
dead("You greedy bastard!")
elif 'none' in choice or 'zero' in choice:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("Man, learn to type a number")
Oh, right! I got it. If the player typed ânone at allâ, the first if bock would have been executed and killed them and would not have gone to the second block. I changed the order of the statements:-
def gold_room():
print("This room is full of gold. How much do you take?)
choice = input('> ')
try:
how_much = int(choice)
if how_much <= 100:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greedy bastard!")
except ValueError:
if 'none' in choice or 'zero' in choice:
print("Nice, you're not greedy, you win!")
exit(0)
elif 'all' in choice:
dead("You greedy bastard!")
else:
dead("Man, learn to type a number")
Now, the player would win cause the first block would save them even if they typed ânone at allâ. Thanks!