Python错误:NameError:未定义全局名称“playerChoice”

时间:2013-09-20 21:22:25

标签: python python-3.x

我正在尝试创建一个简单的Rock,Paper,Scissors游戏,但我总是得到标题中指定的错误。这是代码。第21行弹出错误。我是初学者,所以请保持相对简单。

import random

def displayIntro():
print('Hello! This a game of Rock, Paper, Scissors, against the computer.')
print()

def humanChoice():
playerChoice = ''
while playerChoice != 'Rock' and playerChoice != 'Paper' and playerChoice != 'Scissors':
    print('Please choose: Rock, Paper or Scissors. BTW: Please type your choice with the first letter as a capital.')
    playerChoice = input()

return playerChoice

a = ["Rock", "Paper", "Scissors"]

def computerChoice():
    compChoice = random.choice(a)

def winner():
    if playerChoice == compChoice:
        print('You tied! The computer also chose ' + playerChoice)

    elif playerChoice == Rock and compChoice == Scissors:
    print('You won! You chose ' + playerChoice + ' and the computer chose ' +compChoice)

    elif playerChoice == Paper and compChoice == Rock:
    print('You won! You chose ' + playerChoice + ' and the computer chose ' +compChoice)

    elif playerChoice == Scissors and compChoice == Paper:
    print('You won! You chose ' + playerChoice + ' and the computer chose ' +compChoice)

    else:
    print('You lost! You chose ' + playerChoice + ' and the computer chose ' +compChoice)

playAgain = 'yes'

while playAgain == 'yes' or playAgain == 'Yes':

    displayIntro()

    humanChoice()

    computerChoice()

    winner()

    print('Do you want to play again? (yes or no)')
    playAgain = input()

3 个答案:

答案 0 :(得分:3)

您在代码中遇到了几个问题:

  • 缩进
  • 未定义playerChoicecompChoiceRockPaperScissors变量
  • 代码“闻起来”不好,它不知道PEP8样式指南(对不起,听起来有点粗鲁,我不知道你正在学习python)
  • {li> return函数中没有computerChoice()

但主要问题是未定义的变量。您应该将humanChoice()computerChoice()函数的结果传递给winner()函数,而不是使用全局变量。此外,您应该制作RockPaperScissors字符串。

以下是修改后的代码:

import random


def displayIntro():
    print('Hello! This a game of Rock, Paper, Scissors, against the computer.')
    print()


def humanChoice():
    playerChoice = ''
    while playerChoice != 'Rock' and playerChoice != 'Paper' and playerChoice != 'Scissors':
        print('Please choose: Rock, Paper or Scissors. BTW: Please type your choice with the first letter as a capital.')
        playerChoice = input()

    return playerChoice


def computerChoice():
    return random.choice(["Rock", "Paper", "Scissors"])


def winner(playerChoice, compChoice):
    if playerChoice == compChoice:
        print('You tied! The computer also chose ' + playerChoice)

    elif playerChoice == 'Rock' and compChoice == 'Scissors':
        print('You won! You chose ' + playerChoice + ' and the computer chose ' + compChoice)

    elif playerChoice == 'Paper' and compChoice == 'Rock':
        print('You won! You chose ' + playerChoice + ' and the computer chose ' + compChoice)

    elif playerChoice == 'Scissors' and compChoice == 'Paper':
        print('You won! You chose ' + playerChoice + ' and the computer chose ' + compChoice)

    else:
        print('You lost! You chose ' + playerChoice + ' and the computer chose ' + compChoice)


playAgain = 'yes'
while playAgain in ('yes', 'Yes'):
    displayIntro()
    playerChoice = humanChoice()
    compChoice = computerChoice()
    winner(playerChoice, compChoice)

    print('Do you want to play again? (yes or no)')
    playAgain = input()

答案 1 :(得分:0)

如果你想修改playerChoice,你需要在函数中将它声明为全局,并将它声明在你想要使用它的第一个位置上方:

playerChoice = ''

def humanChoice():
    global playerChoice
    playerChoice = '...'

请注意,这不是一个很好的编程策略,因为它使用全局变量(http://en.wikipedia.org/wiki/Global_variable)。

答案 2 :(得分:0)

您可以在函数之外声明它:

playerChoice = ''
def humanChoice():

希望有所帮助