繁体   English   中英

为什么即使我添加了一种重新启动它的方法,我的代码也会结束

[英]Why my code ends even though i add a way to restart it

def main(): main()这是我再次询问有关我的代码的问题

C:\Users\Timothy\Documents>python text.py

C:\Users\Timothy\Documents>

这是我的代码

def main():
    import random
    print("1=Rock, 2=Paper, 3=Scissor")
    player = int(input("Please Put your choice"))
    ai = random.randint(1,3)
        ###if and else statement for ai to print the random integer to string
    if (ai == 3):
        print("AI: Scissor")
    elif (ai == 2):
        print("AI: Paper")
    elif (ai == 1):
        print("AI: Rock")
            ####if and statement for the ai and player
    if (player == ai):
        print("It is a tie")
    if (player == 1 and ai == 2):
        print("AI won")
    if (player == 1 and ai == 3):
         print("You Won!!")
    if (player == 2 and ai == 1):
        print("You won")
    if (player == 2 and ai == 3):
        print("AI won")
    if (player == 3 and ai == 1):
        print("AI won")
    if (player == 3 and ai == 2):
        print("You won")

        main()






是的,缩进太多了

def main():
    import random
    print("1=Rock, 2=Paper, 3=Scissor")
    player = int(input("Please Put your choice"))
    ai = random.randint(1,3)
        ###if and else statement for ai to print the random integer to string
    if (ai == 3):
        print("AI: Scissor")
    elif (ai == 2):
        print("AI: Paper")
    elif (ai == 1):
        print("AI: Rock")
            ####if and statement for the ai and player
    if (player == ai):
        print("It is a tie")
    if (player == 1 and ai == 2):
        print("AI won")
    if (player == 1 and ai == 3):
         print("You Won!!")
    if (player == 2 and ai == 1):
        print("You won")
    if (player == 2 and ai == 3):
        print("AI won")
    if (player == 3 and ai == 1):
        print("AI won")
    if (player == 3 and ai == 2):
        print("You won")

        main() # this way if will run if this condition is met player == 3 and ai == 2
    main() # this will surely run in the end calling itself

这是一种使用while True循环程序的方法,以便可以多次玩游戏。

def main():
    import random
    print("1=Rock, 2=Paper, 3=Scissor")
    player = int(input("Please Put your choice"))
    ai = random.randint(1,3)
        ###if and else statement for ai to print the random integer to string
    if (ai == 3):
        print("AI: Scissor")
    elif (ai == 2):
        print("AI: Paper")
    elif (ai == 1):
        print("AI: Rock")
            ####if and statement for the ai and player
    if (player == ai):
        print("It is a tie")
    if (player == 1 and ai == 2):
        print("AI won")
    if (player == 1 and ai == 3):
         print("You Won!!")
    if (player == 2 and ai == 1):
        print("You won")
    if (player == 2 and ai == 3):
        print("AI won")
    if (player == 3 and ai == 1):
        print("AI won")
    if (player == 3 and ai == 2):
        print("You won")


while True:
    main()
    restart = input("Again? Y/N: ").upper()
    if restart == "Y":
        main()
    if restart == "N":
        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM