【发布时间】:2017-08-14 13:47:20
【问题描述】:
谁能帮我理解为什么我的非常简单的石头剪刀布代码会卡住并在第 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()
【问题讨论】:
-
尝试打印battle的值,并将其与连续if语句中的值进行比较。
-
您是否有重复该问题的条目?
-
提供的代码有很多问题。有些已经在答案中解释了,有些没有。例如,您拥有
.lower和.upper的任何地方都应该是lower()和upper()。否则这些if条件永远不会是True。 -
非常感谢 DeepSpace,你们的 cmets 让我在这个程序上取得了长足的进步。现在一切都很完美,Python就是这样一个语法纳粹:P