【问题标题】:why is my python code not running fully为什么我的 python 代码没有完全运行
【发布时间】: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

标签: python exit break


【解决方案1】:

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

playagain() 没有被调用,因为没有一个条件为真。

【讨论】:

    【解决方案2】:

    错误在于:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2019-04-23
      • 2020-10-28
      相关资源
      最近更新 更多