石头,布,剪刀游戏

2024-05-13 18:17:57 发布

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

import random

def main():
playagain = 1
win=0
lose=0
tie=0      
while playagain==1:
    printCpu=cpuConverter()
    printPlayer=playerConverter()
    print("The computers choice is", printCpu)
    print("Your choice is", printPlayer)
    gameWinner(printCpu, printPlayer)
    winner(gameWinner, printCpu, printPlayer)
    playagain=int(input("Would you like to play again? Enter 1 for yes, any other number for no!"))

print("Your total wins are", win)
print("Your total losses are", lose)
print("Your total ties are", tie)



def cpuConverter():
    cpuChoice=random.randrange(1,4)
    if cpuChoice==1:
        printCpu="Rock"
    elif cpuChoice==2:
        printCpu="Paper"
    else:
        printCpu="Scissors"
    return printCpu

def playerConverter():
    again=0
    while again<1 or again>3:
        printPlayer=int(input("Please choose a number to play against the      computer. 1=Rock 2=Paper 3=Scissors "))
        if printPlayer<1 or printPlayer>3:
            print("Invalid choice for the game. Please choose another number inside the constraints.")
        elif printPlayer==1:
            printPlayer="Rock"
            again=1
        elif printPlayer==2:
            printPlayer="Paper"
            again=1
        else:
            printPlayer="Scissors"
            again=1
    return printPlayer

def gameWinner(printCpu, printPlayer):
    if printCpu == printPlayer:
        winner = "tie"
        print("It's a tie")
    elif printCpu == "Scissors" and printPlayer == "Rock":
        winner = "win"
        print("Rock beats Scissors! You win")
    elif printCpu == "Paper" and printPlayer == "Scissors":
        winner = "win"
        print("Scissors cuts paper! You win")
    elif printCpu == "Rock" and printPlayer == "Paper":
        winner = "win"
        print("Paper covers Rock! You win")
    else:
        winner = "lose"
        print("You lose")
    return winner

def winner(gameWinner, printCpu, printPlayer):     
    if winner == "win":
        win += 1
    elif winner == "lose":
        lose += 1
    elif winner == "tie":
        tie += 1
    return winner

main()

所以当我尝试这个代码时,大部分情况下一切都正常。我唯一不能工作的就是实际的计数部分。当我在玩了多次(甚至一次)后试图打印我的结果时,代码仍然以零作为游戏总量。有人能告诉我我哪里出了问题,希望能帮我解决这个问题吗?在


Tags: yourdefwinpaperprintscissorsrockelif
1条回答
网友
1楼 · 发布于 2024-05-13 18:17:57

你的代码中有几个错误。首先是

winner(gameWinner, printCpu, printPlayer)

function传递给函数winner。您应该捕获gameWinner的返回值并将其传递给winner

^{pr2}$

下一期

def winner(gameWinner, printCpu, printPlayer):     
    if winner == "win":

您忽略了输入参数,并将function本身与字符串“win”进行比较。它将始终是False。这样做的结果是你的winlosetie变量永远不会被触及。在

最后一个问题是winlose和{}是全局的。所有有经验的程序员都不赞成使用全局变量,但如果必须使用全局变量,则应首先在全局范围内声明变量,而不是在函数main中声明变量。然后应该在引用它们的任何函数中使用global关键字。所以在winner内我们需要

global win, lose, tie

经过最少修改的代码看起来像

import random

win=0
lose=0
tie=0 

def main():
    playagain = 1     
    while playagain==1:
        printCpu=cpuConverter()
        printPlayer=playerConverter()
        print("The computers choice is", printCpu)
        print("Your choice is", printPlayer)
        result = gameWinner(printCpu, printPlayer)
        winner(result, printCpu, printPlayer)
        playagain=int(input("Would you like to play again? Enter 1 for yes, any other number for no!"))

    print("Your total wins are", win)
    print("Your total losses are", lose)
    print("Your total ties are", tie)



def cpuConverter():
    cpuChoice=random.randrange(1,4)
    if cpuChoice==1:
        printCpu="Rock"
    elif cpuChoice==2:
        printCpu="Paper"
    else:
        printCpu="Scissors"
    return printCpu

def playerConverter():
    again=0
    while again<1 or again>3:
        printPlayer=int(input("Please choose a number to play against the      computer. 1=Rock 2=Paper 3=Scissors "))
        if printPlayer<1 or printPlayer>3:
            print("Invalid choice for the game. Please choose another number inside the constraints.")
        elif printPlayer==1:
            printPlayer="Rock"
            again=1
        elif printPlayer==2:
            printPlayer="Paper"
            again=1
        else:
            printPlayer="Scissors"
            again=1
    return printPlayer

def gameWinner(printCpu, printPlayer):
    if printCpu == printPlayer:
        winner = "tie"
        print("It's a tie")
    elif printCpu == "Scissors" and printPlayer == "Rock":
        winner = "win"
        print("Rock beats Scissors! You win")
    elif printCpu == "Paper" and printPlayer == "Scissors":
        winner = "win"
        print("Scissors cuts paper! You win")
    elif printCpu == "Rock" and printPlayer == "Paper":
        winner = "win"
        print("Paper covers Rock! You win")
    else:
        winner = "lose"
        print("You lose")
    return winner

def winner(gameWinner, printCpu, printPlayer):  
    global win, lose, tie   
    if gameWinner == "win":
        win += 1
    elif gameWinner == "lose":
        lose += 1
    elif gameWinner == "tie":
        tie += 1
    return winner

main()

相关问题 更多 >