为什么我的 python 代码没有 运行 完整

why is my python code not running fully

谁能帮我理解为什么我非常简单的剪刀石头布代码会卡住并在第 18 行末尾退出? 我单独测试了每个部分并且它可以工作,它可能不是最漂亮的代码但它似乎完成了工作,但是在它的最新迭代中它只是在第 18 行的结尾退出,退出代码 0,所以没有错误, 没有说有什么不对,它只是不执行下一行,就像那一行有一个中断或退出,但没有:

 import random

def startgame():
    print("Please choose rock - r, paper - p or scissors - s:")
    pchoice = input(str())
    if(pchoice.lower in ["r","rock"]):
        pchoice = "0"
    elif(pchoice.lower in ["s","scissors"]):
        pchoice = "1"
    elif(pchoice.lower in ["p","paper"]):
        pchoice = "2"
    cchoice = (str(random.randint(0,2)))
    if(cchoice == "0"):
        print("Computer has chosen: Rock \n")
    elif(cchoice == "1"):
        print("Computer has chosen: Scissors \n")
    elif(cchoice == "2"):
        print("Computer has chosen: Paper \n")
#runs perfect up to here, then stops without continuing
    battle = str(pchoice + cchoice)
    if(battle == "00" and "11" and "22"):
        print("Draw! \n")
        playagain()
    elif(battle == "02" and "10" and "21"):
        if(battle == "02"):
            print("You Lose! \nRock is wrapped by paper! \n")
        elif(battle == "10"):
            print("You Lose! \nScissors are blunted by rock! \n")
        elif(battle == "21"):
            print("You Lose! \nPaper is cut by scissors! \n")
            playagain()
    elif(battle == "01" and "12" and "20"):
        if(battle == "01"):
            print("You Win! \nRock blunts scissors! \n")
        elif(battle == "12"):
            print("You Win! \nScissors cut paper! \n")
        elif(battle == "20"):
            print("You Win! \nPaper wraps rock! \n")
            playagain()

def main():
    print("\nWelcome to Simon´s Rock, Paper, Scissors! \n \n")
    startgame()

def playagain():
        again = input("Would you like to play again? y/n \n \n")
        if(again == "y"):
            startgame()
        elif(again == "n"):
            print("Thank you for playing")
            exit()
        else:
            print("Please choose a valid option...")
        playagain()

main()

错误在这里:

if(battle == "00" and "11" and "22"):

这将在所有情况下计算为 False,但 00,您需要将其更改为:

if battle == "00" or battle == "11" or battle == "22":

以及您使用 and

的其他两个语句

您的陈述被解释如下:

True/False 1- if battle == "00" 
True       2- and "11" #<-- here it checks if the string is True which means string is not empty
True       3- and "22" is True #<-- here too

所以您的语句只有在所有语句都是 True 时才有效,因为您使用的是 and,这要求语句的所有部分都是 True。第二和第三部分总是 True 所以它检查选择是否是 "00"

你需要的是:

1- if battle == "00" True/False
2- or battle == "11" True/False
3- or battle == "22" True/False

你只需要一个部分True,因为or

在像这样的行中 if(battle == "00" and "11" and "22"): 使用 in 运算符 if(battle in ["00", "11", "22"]):

playagain() 未被调用,因为 none 的条件为真。