Skip to content Skip to sidebar Skip to footer

Python Blackjack Game, Seems To Be Ignoring 'if' Statement

I've been trying to make a simple blackjack game in python and I seem to be stuck, my code is as follows: from random import choice def deck(): cards = range(1, 12) return

Solution 1:

if "hit" just tests if the string "hit" exists, and it does. Thus, the elif statement is never executed.

You need to capture the user input in a variable and test against that instead:

choice = raw_input("Would you like to hit or stand?")
print choice
if choice == "hit":
    return hand + deck()
elif choice == "stand": 
    return hand

Solution 2:

Assuming you get the indentation right:

print raw_input("Would you like to hit or stand?")
if"hit":
    return hand + deck()
elif"stand": 
    return hand

Your if is just checking whether the string "hit" is true. All non-empty strings are true, and "hit" is non-empty, so this will always succeed.

What you want is something like this:

cmd = raw_input("Would you like to hit or stand?")
ifcmd== "hit":
    return hand + deck()
elifcmd== "stand": 
    return hand

Now you're checking whether the result of raw_input is the string "hit", which is what you want.

Post a Comment for "Python Blackjack Game, Seems To Be Ignoring 'if' Statement"