【问题标题】:Why is the function not displaying a winner?为什么该功能不显示获胜者?
【发布时间】:2018-11-14 23:20:37
【问题描述】:

游戏需要这些功能,并且可以运行,但不会显示获胜者。我不知所措,因为这是明天晚上到期的。今天是分配给我的。我已经尽我所能,我真的不知道该怎么做。

游戏由电脑随机选择13 运行。 1 是石头,2 是纸,3 是剪刀。计算机选项不需要在开始时显示。然后用户应该输入石头纸或剪刀。之后将显示计算机的选择。并根据石头剪刀布的基本规则选出获胜者。如果两位玩家的答案相同,则视为平局。

对于我的成绩,它必须具有 main()get_ComputerMove()get_PlayerMove()calculateWinner() 的功能。提前致谢。

import random


def startAgain():
    randomNumber = getRandomNumber()
    computerChoice = get_ComputerMove(randomNumber)
    userChoice = get_PlayerMove()
    print('The computer chose', computerChoice )
    winner, message = calculateWinner(computerChoice,userChoice )
    if winner != 'no winner':
        print(winner,'won(',message, ')')        

def getRandomNumber():
    randomNumber = random.randint( 1, 3 )
    return randomNumber

def get_ComputerMove( randomNumber ):
    if randomNumber == 1:
        computerChoice = "rock"
    elif randomNumber == 2:
        computerChoice = "paper"
    else:
        computerChoice = "scissors"

    return computerChoice
def get_PlayerMove():
    userChoice = input("Please enter your choice")
    return userChoice

def calculateWinner( computerChoice, userChoice ):
    rockMessage = "The rock smashes the scissors"
    scissorsMessage = "Scissors cuts paper"
    paperMessage = "Paper covers the rock"
    winner = "no winner"
    message = ""
    if computerChoice == "rock" and userChoice == "scissors":
       winner = "Computer"
       message = rockMessage
    elif computerChoice == "scissors" and userChoice == "rock":
       winner = "you"
       message = rockMessage
    if computerChoice == "scissors" and userChoice == "paper":
       winner = "Computer"
       message = scissorsMessage
    elif computerChoice == "paper" and userChoice == "scissors":
       winner = "you"
       message = scissorsMessage
    if computerChoice == "paper" and userChoice == "rock":
       winner = "Computer"
       message = paperMessage
    elif computerChoice == "rock" and userChoice == "paper":
       winner = "you"
       message = paperMessage                 
    return winner, message 
def main():
    randomNumber = getRandomNumber()
    computerChoice = get_ComputerMove(randomNumber)
    userChoice = get_PlayerMove()
    print("The computer chose" , computerChoice )
    winner,message = calculateWinner( computerChoice,userChoice )
    if winner != "no winner":
        print(winner,"won(",message, ")")
    while winner == "no winner":
        print('You both chose the same thing')
        winner = startAgain()       
main() 

【问题讨论】:

  • 实际运行正常,请输入您的选择纸计算机选择了您赢得的岩石(纸盖住了岩石)
  • 对我来说似乎工作正常。
  • 欢迎来到 Stack Overflow!请在您的问题中将正文文本格式化为正文文本,而不是标题。
  • 我才意识到问题是每当我输入一些东西时,我自己添加了一个空格,它真的只是把我的程序弄乱了。我回去编辑了代码,所以在它要求选择之后有一个空格。所以不是显示为请输入您的选择纸,而是显示为请输入您的选择纸。它现在可以正常工作了。
  • 从技术上讲,正如这里所写的,如果你与计算机打成平局两次,它实际上就坏了(在这种情况下它不会宣布获胜)。 startAgain 始终返回 None,这意味着您的 while winner == "no winner" 子句在单次迭代后将始终为 True。我赞成 Vitor 的回答,因为它解决了这个问题并导致总是宣布获胜者(根据原始问题)。

标签: python-3.x function while-loop


【解决方案1】:

如果您想重复游戏直到获胜,while循环可以解决问题:

def main():
    winner = "no winner"

    while winner == "no winner":
        randomNumber = getRandomNumber()
        computerChoice = get_ComputerMove(randomNumber)
        userChoice = get_PlayerMove()

        print("The computer chose", computerChoice)
        winner, message = calculateWinner(computerChoice, userChoice)

        if winner != "no winner":
            print(winner, "won(", message, ")")
        else:
            print('You both chose the same thing')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多