首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我的代码结束了,即使我添加了一种重启它的方法

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

Stack Overflow用户
提问于 2019-09-30 14:40:38
回答 2查看 46关注 0票数 0

您好,这是我再次询问关于我的代码的问题我正在创建一个石头,纸,剪刀我添加了main()def main():,并缩进了代码,但当我运行代码时发生了这种情况

代码语言:javascript
运行
复制
C:\Users\Timothy\Documents>python text.py

C:\Users\Timothy\Documents>

这是我的代码

代码语言:javascript
运行
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-30 14:51:42

下面是一种使用while True循环程序的方法,这样游戏就可以多次运行。

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2019-09-30 14:45:38

是的,缩进得太多了。

代码语言:javascript
运行
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58162652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档