-1

We're making blackjack and when we ask hit or stay, how do we make sure that the player gives us a valid response by typing either 'hit' or 'stay'?

Thanks!

4
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by jcao219
0

You could assign numbers as responses. Like press 1 to hit and 0 to stay.

def call():
    stand = input("Press '1' to STAY or '0' to HIT")
    if stand == 1:
        do something
    elif stand == 0:
        do something else
    else:
        call()
0

If you are using python 3 then the above code will not work. What you will need to do then is slightly different as input() in python 3 returns a string.

choice = input("Press 1 to hit or 0 to stay")
if int(choice) == 1:
    print "Lets hit it!"
elif int(choice) == 0:
    print "nothin doing"

Hope that helps :)

-1

try this:

response = input("Hit or stay: ").lower()
if (response != "hit") and (response != "stay"):
    #bad response
    pass
else:
    #good response
    pass

the above post might not work since if the user types something other than number,
int() won't work, and there will be an exception.

Edited by jcao219: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.