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!
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()
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 :)
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.