Python 如何将自己的函数放在if语句中?

Python 如何将自己的函数放在if语句中?,python,function,if-statement,statements,blackjack,Python,Function,If Statement,Statements,Blackjack,我正在尝试制作一个简单的21点游戏,我希望能够创建我自己的函数,a已经可以这样做了,然后将其放入if语句中,这样,如果用户想要“站立”,那么它将运行函数到“站立”。然而,当python通读代码时,即使用户说“Hit”,它也会看到所有函数并运行所有函数 def stand(): print("You have chosen to Stand") #my stand code will go here def hit(): print("You have chosen to

我正在尝试制作一个简单的21点游戏,我希望能够创建我自己的函数,a已经可以这样做了,然后将其放入if语句中,这样,如果用户想要“站立”,那么它将运行函数到“站立”。然而,当python通读代码时,即使用户说“Hit”,它也会看到所有函数并运行所有函数

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()
我希望用户能够一次运行'Hit'、'double down'或'stand'功能1,而不必全部运行

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()

问题出在if语法中。

因为这里错误地使用了if语句,如“if step==(“Hit”)或(“Hit”):” python将执行step==(“Hit”)并根据用户输入结果为False或True,但它遵循字符串“Hit”,python将像True一样读取此值,因此,在最后,它就像“if(step==(“Hit”))或(True),那么您的每个if语句都将执行,因为逻辑上为True

您应该像这样更改代码
if(step==sth)或(step==sth)

如前所述,您需要if、elif…else流。唯一的添加是使用.lower()减少重复:例如,if在step.lowe()中“hit”:…@jornsharpe:这不是重复,因为这里有另一个问题:变量
step
和函数
step()的名称冲突
@John然后他们有两个问题,但他们会先击中被骗者,这就是他们描述的问题。”或者“不是那样的。你必须在每一边都有完整的条件:'或者(“站立”)'总是正确的,所以所有的如果都会运行。