Python cod输出错误

2024-10-02 22:25:57 发布

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

好的,我是一个编程初学者(显然是Python),我的代码有问题。我刚刚自己写的代码,使用所有的东西,我学到的教程。我正在做一个石头布剪刀游戏。实际上,它正在工作,但是当我按下numpad'3'时,它会显示my else语句中的代码。有人能帮我吗?你知道吗

import random

playerScore = 0
cpuScore = 0

def rpsGame():

    userInput = int(input("Choose (1)rock (2)paper (3)scissors: "))

    global playerScore
    global cpuScore
    cpuGuess = random.randint(1, 4)

    if userInput == 1 and cpuGuess == 1:
        print("A draw! rock with a rock!")
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 1 and cpuGuess == 2:
        print("CPU scores! paper never beats rock!")
        cpuScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 1 and cpuGuess == 3:
        print("Player scores! rock beats sharp scissors!")
        playerScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 2 & cpuGuess == 1:
        print("Player! paper never beats rock!")
        playerScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 2 & cpuGuess == 2:
        print("A draw! paper with a paper!")
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 2 & cpuGuess == 3:
        print("CPU scores! paper is cut with scissors!")
        cpuScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 3 & cpuGuess == 1:
        print("CPU scores! scissors can't cut rock!")
        cpuScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 3 & cpuGuess == 2:
        print("Player scores! Scissors beat paper")
        playerScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)

    elif userInput == 3 & cpuGuess == 3:
        print("A draw! scissors with scissors!")
        print("Player Score:", playerScore, "CPU Score:", cpuScore)    
    else:
        print("Error")


while playerScore != 3 or cpuScore != 3:
    rpsGame()

if playerScore >= 3:
    print("Player wins!")

if cpuScore >= 3:
    print("You lose! The opponent won!")

the python shell with the outputs of my program

在帖子里有一个链接,是“错误”的图片。我知道,我在最后的else语句中写了错误,所以我会知道是否有任何问题。 抱歉,我的“else-if”语句太长了,我会在观看和阅读更多关于“else-if”的教程时更改它Python。谢谢供考虑:D


Tags: ifwithcpuelsepaperscoreplayerprint
3条回答

这应该是可行的(我添加了一些代码,只允许输入1、2或3的数字)。你知道吗

import random

playerScore = 0
cpuScore = 0

def rpsGame():
    while True:
        try:
            userInput = int(input("Choose (1)rock (2)paper (3)scissors: "))
        except ValueError:
            print("That\'s not a number!")
        else:
            if 1 <= userInput < 3: 
                break
            else:
                print("Out of range. Try again")

    global playerScore
    global cpuScore
    cpuGuess = random.randint(1, 3)

    if userInput == 1 and cpuGuess == 1:
        print("A draw! rock with a rock!")
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 1 and cpuGuess == 2:
        print("CPU scores! paper never beats rock!")
        cpuScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 1 and cpuGuess == 3:
        print("Player scores! rock beats sharp scissors!")
        playerScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 2 and cpuGuess == 1:
        print("Player! paper never beats rock!")
        playerScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 2 and cpuGuess == 2:
        print("A draw! paper with a paper!")
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 2 and cpuGuess == 3:
        print("CPU scores! paper is cut with scissors!")
        cpuScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 3 and cpuGuess == 1:
        print("CPU scores! scissors can't cut rock!")
        cpuScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 3 and cpuGuess == 2:
        print("Player scores! Scissors beat paper")
        playerScore += 1
        print("Player Score:", playerScore, "CPU Score:", cpuScore)
    elif userInput == 3 and cpuGuess == 3:
        print("A draw! scissors with scissors!")
        print("Player Score:", playerScore, "CPU Score:", cpuScore)    
    else:
        print("Error")

while playerScore != 3 or cpuScore != 3:
    rpsGame()

if playerScore >= 3:
    print("Player wins!")

if cpuScore >= 3:
    print("You lose! The opponent won!")

你的CPU猜测是CPU猜测在1和4之间。将4替换为3。你知道吗

cpuGuess = random.randint(1, 4)

应该是:

cpuGuess = random.randint(1, 3)

看看这个随机.randint功能。可悲的是,这与范围或切片不同。所以它进入else语句的原因是cpuGuess==4。你知道吗

另外,看看PEP8,我不知道为什么,但我只是有点畏缩,每次我看到这个upy唐尼丘陵案件。你知道吗

相关问题 更多 >