为什么该功能不显示赢家?

时间:2018-11-14 23:20:37

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

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

该游戏通过计算机从1随机选择3的方式运行。 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() 

1 个答案:

答案 0 :(得分: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')