Ex35 study drill 5

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.

1 Like

Now, what if they type none at all? :wink:

Unfortunately, the logic will kill you for being “greedy” just as if you’d tried to take all the gold!

@florian Oh… Yeah you’re right, I can’t just kill folk if they aren’t greedy… :man_facepalming: 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")

@Arrarr Yeah, I forgot to copy a ‘d’, sorry :sweat_smile:

2 Likes

:smiley: That’s very kind of you. Only, I’m not sure they would survive this one either…

I tried seeing what is still wrong but I can’t figure it out. Please tell my mistake or a hint :sweat_smile:

P.S. Sorry for the late replies. Just been busy these days.

@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")

It’s the order of the if statements. Think it through, will your elif block really save them if they type ‘none at all’?

1 Like

Oh, right! :man_facepalming: 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. :thinking: 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! :grin:

1 Like