0

我正在尝试创建一个简单的 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()
4

3 回答 3

3

您在代码中有几个问题:

  • 缩进
  • 未定义playerChoice, compChoice, Rock, Paper,Scissors变量
  • 代码“闻起来”很糟糕,它不知道PEP8风格指南(对不起,这听起来可能有点粗鲁,我不明白你正在学习 python)
  • 没有功能return_computerChoice()

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

这是经过修改的代码:

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()
于 2013-09-20T21:30:19.783 回答
0

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

playerChoice = ''
def humanChoice():

希望它有帮助

于 2013-09-20T21:35:27.510 回答
0

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

playerChoice = ''

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

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

于 2013-09-20T21:26:17.623 回答