Python:我的代码在应该完成之后会自动重复

2024-05-14 15:16:16 发布

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

我正在用Python编写一个石头、布、剪刀的游戏,但是我的代码没有正常工作。我是Python新手,所以如果我的代码格式不正确,请告诉我。游戏运行良好,假设您输入一个已经存在的答案。但是,如果输入一个不同的值,则在执行'end()'函数后,代码似乎会随机循环。你知道吗

这是我的密码:

# imports needed files
from random import randint
import time
# creates a function that ends the game
def end(cpuScore,playerScore):
    time.sleep(1)
    cont = input("Would you like to play again? (y or n)\n")
    if cont=="y":
        time.sleep(1)
        start()
    else:
        print("Well... That's a bit rude.")
# creates a function to play the game
def rps(cpuScore,playerScore,num):
    # loops code 3 times (unless 'num' is different)
    for x in range(num):
        num-=1
        # creates options
        options = ["rock","paper","scissors"]
        # picks a random choice for cpu
        cpu = options[randint(0,2)]
        # asks the player to choose
        player = input("rock, paper or scissors?\n")
        # why not gun?
        if player=="gun":
            result = "w"
        elif player==cpu:
            result = "d"
        elif player=="rock":
            if cpu=="paper":
                result = "l"
            if cpu=="scissors":
                result = "w"
        elif player=="paper":
            if cpu=="scissors":
                result = "l"
            if cpu=="rock":
                result = "w"
        elif player=="scissors":
            if cpu=="rock":
                result = "l"
            if cpu=="paper":
                result = "w"
        # if they choose something other than rock, paper, scissors or gun
        else:
            print("That's an invalid input!")
            # adds one to num so that this round is not counted as one of the 3
            num+=1
            # plays the game again with the amount of rounds remaining
            rps(cpuScore,playerScore,num)

        # tells the player how they did
        if result=="w":
            playerScore+=1
            time.sleep(1)
            print("Fine! You win! Your silly " + player + " beat my " + cpu + "!!!")
        if result=="l":
            cpuScore+=1
            time.sleep(1)
            print("Ha! Sucker!! My epic " + cpu + " smashed your measly " + player + "!!!")
        if result=="d":
            time.sleep(1)
            print("Ah! We drew by both choosing %s! Like they say, great minds think alike!" % cpu)
        # announces the scores
        print("You are on %s and the computer is on %s!" % (playerScore,cpuScore))
    # ends the game after 3 rounds
    end(cpuScore,playerScore)
# creates the funtion that sets the variables and starts the game
def start():
    result=""
    cont=""
    cpuScore=0
    playerScore=0
    rps(cpuScore,playerScore,3)
# begins the game
start()

谢谢你, 里斯库姆斯


Tags: thegameiftimesleepresultcpunum
1条回答
网友
1楼 · 发布于 2024-05-14 15:16:16

基本上你的rps函数循环num次,最初是num = 3。如果用户输入了不正确的输入,则回调函数,,该函数在新上下文中再次启动整个过程num+1次。你知道吗

因此,如果你第一次回答错误,你至少有六个游戏要玩:四个新增加的和两个最初的你没有尝试玩。你知道吗

我的建议是先做一个程序,只做一次石头剪纸游戏的迭代。添加更多迭代是添加全局循环的一个简单事实。你知道吗

相关问题 更多 >

    热门问题