Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么功能不显示赢家?_Python 3.x_Function_While Loop - Fatal编程技术网

Python 3.x 为什么功能不显示赢家?

Python 3.x 为什么功能不显示赢家?,python-3.x,function,while-loop,Python 3.x,Function,While Loop,游戏需要这些功能,它可以工作,但不会显示赢家。我已经不知所措了,因为明天晚上就要交了。今天分配给我的。我已经尽力了,但我真的一点也不知道该做什么 游戏由计算机通过3随机选择11是石头,2是纸,3是剪刀。计算机选项不需要在开始时显示。然后用户应该输入石头纸或剪刀。之后,将显示计算机的选择。根据石头剪纸的基本规则选出获胜者。如果两名球员的答案相同,则视为平局 对于我的成绩,它必须有函数main(),get\u ComputerMove(),get\u PlayerMove(),和calculateW

游戏需要这些功能,它可以工作,但不会显示赢家。我已经不知所措了,因为明天晚上就要交了。今天分配给我的。我已经尽力了,但我真的一点也不知道该做什么

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

对于我的成绩,它必须有函数
main()
get\u ComputerMove()
get\u 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() 

如果你想重复游戏,直到它有一个赢家,while loop会执行以下操作:

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')

它的工作原理很好,请输入您的选择文件计算机选择岩石你赢了(纸覆盖岩石)对我来说似乎工作得很好。欢迎使用堆栈溢出!请将主体文本格式化为问题中的主体文本,而不是标题。我刚刚意识到问题是,每当我输入某个内容时,我自己都添加了一个空格,这真的会弄乱我的程序。我返回并编辑了代码,以便在它请求后有一个空格选择。因此,它不是显示为请输入您的选择纸,而是显示为请输入您的选择纸。而且它现在工作正常。从技术上讲,如本文所述,如果您两次与计算机连接,它实际上会断开(在这种情况下,它不会宣布获胜).
startAgain
总是返回
None
,这意味着你的
while winner==“no winner”
子句在一次迭代后总是
True
。我对Vitor的答案投了赞成票,因为它解决了这个问题,结果总是会宣布一个获胜者(根据原始问题)。