谁能帮我理解为什么我非常简单的石头剪刀布代码卡在第 18 行末尾并退出? 我已经单独测试了每个部分并且它可以工作,它可能不是最漂亮的代码,但它似乎可以完成这项工作,但是在它的最新迭代中,它只是在第 18 行的 en 处退出,退出代码 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"):
使用in
运算符if(battle in ["00", "11", "22"]):
playagain()
没有被调用,因为没有一个条件是真的。
错误在于:
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