Python代码运行两次

2024-05-03 11:38:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python制作一个石头布剪刀游戏,同时尝试实现一个胜负点数系统。当我运行代码,我被要求选择石头布或剪刀,我选择我的答案,但后来我又被问了一些原因,有人能帮我吗?(我基本上是个初学者)

from random import randint
def RPS():
    UserPts = 0
    AIPts = 0
    def Game():
        moves = ["Rock","Paper","Scissors"]
        def genAImove():
            genai = moves[randint(0,2)]
            return genai
        genAImove()
        AImove = genAImove()
        def genUserMove():
            genu = raw_input("Choose your move ('Rock','Paper' or 'Scissors')\n")
            return genu
        genUserMove()
        UserMove = genUserMove()        
        if UserMove == "Rock" and AImove == "Rock":
            print "The AI chose rock too.\nDraw."
            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Rock" and AImove == "Paper":
            print "The AI chose paper.\nLoss."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Rock" and AImove == "Scissors":
            print "The AI chose scissors.\nWin."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Paper" and AImove == "Rock":
            print "The AI chose rock.\nWin."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Paper" and AImove == "Paper":
            print "The AI chose paper.\nDraw."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Paper" and AImove == "Scissors":
            print "The AI chose scissors.\nLoss."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Scissors" and AImove == "Rock":
            print "The AI chose rock.\nLoss."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Scissors" and AImove == "Paper":
            print "The AI chose paper.\nWin."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
        if UserMove == "Scissors" and AImove == "Scissors":
            print "The AI chose scissors.\nDraw."

            def cont():
                cnt = raw_input("Do you want to continue? (Y\N)")
                if cnt == "Y":
                    Game()
                elif cnt == "N":
                    exit
                else:
                    print "Pick Y or N"
                    cont()
            cont()
    Game()
RPS()

Tags: orandthegameinputrawifdef
1条回答
网友
1楼 · 发布于 2024-05-03 11:38:10

在这些行中:

genUserMove()
UserMove = genUserMove() 

首先调用genUserMove,它会询问您,然后再调用它,并分配结果。你知道吗

把第一行去掉。你知道吗

相关问题 更多 >