Python代码运行两次

时间:2016-03-19 17:00:30

标签: python repeat

我正试图在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()

1 个答案:

答案 0 :(得分:2)

在这些方面:

genUserMove()
UserMove = genUserMove() 

首先请求genUserMove,然后再次调用,然后分配结果。

只需删除第一行。