-1

I am trying to make a simple blackjack game and i want to be able to create my own function, which a can already do, and then put it inside an if statement so that if the user want's to 'stand' then it runs the function to 'stand'. However, when python reads through the code, even if the user say 'Hit', it see's all the functions and just runs all of them.

def stand():
    print("You have chosen to Stand")
    #my stand code will go here
def hit():
    print("You have chosen to Hit")
    #my Hit code will go here
def doubledown():
    print("You have chosen to Double Down")
    #my Double Down code will go here
def step():
    step = input("What would you like to do now? Stand, Hit or Double Down")
    if step == ("Stand") or ("stand"):
        stand()
    if step == ("Hit") or ("hit"):
        hit()
    if step == ("Double down") or ("down"):
        doubledown()
    else:
        step()
step()

I would like the user to be able to run the 'Hit', 'double down' or 'stand' function 1 at a time without all of them running.

  • As mentioned, you need if, elif ... else flow. The only addition is using .lower() to reduce repetition: eg. if 'hit' in step.lowe(): ... – Prayson W. Daniel Dec 30 '18 at 09:21
  • @jonrsharpe: This is not a duplicate, since here is another issue: We have a name clash of variable `step` and function `step()`. – John Dec 30 '18 at 09:47
  • @John then they have two problems, but they'll hit the dupe first and that's the one they're describing. – jonrsharpe Dec 30 '18 at 09:57
  • "or" doesn't work that way. You have to have complete conditions on each side: 'or ("stand")' is always true, so all of your ifs run. – Lee Daniel Crocker Jan 08 '19 at 19:58

2 Answers2

0
def stand():
    print("You have chosen to Stand")
    #my stand code will go here
def hit():
    print("You have chosen to Hit")
    #my Hit code will go here
def doubledown():
    print("You have chosen to Double Down")
    #my Double Down code will go here
def step():
    step = input("What would you like to do now? Stand, Hit or Double Down")
    if (step == "Stand" or step== "stand"):
        stand()
    elif (step == "Hit" or step=="hit"):
        hit()
    elif (step == "Double down" or step=="down"):
        doubledown()
    else:
        step()
step()

The problem was in if syntax.

Ashu Grover
  • 737
  • 1
  • 11
  • 26
0

because of wrong use of if statements here like this “if step == ("Hit") or ("hit"):” python will do step==(“Hit”) and it turn out False or True depending on user input but it follow string “hit” python will read this like True so ,at the end, it is like “if (step==(“Hit”)) or (True) ,then your every if statements will be done because logically True !

you should change your code like if (step==sth) or (step==sth)

Gary Yu
  • 21
  • 1
  • 2