我正在尝试创建一个简单的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()
答案 0 :(得分:3)
您在代码中遇到了几个问题:
playerChoice
,compChoice
,Rock
,Paper
,Scissors
变量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()
答案 1 :(得分:0)
如果你想修改playerChoice,你需要在函数中将它声明为全局,并将它声明在你想要使用它的第一个位置上方:
playerChoice = ''
def humanChoice():
global playerChoice
playerChoice = '...'
请注意,这不是一个很好的编程策略,因为它使用全局变量(http://en.wikipedia.org/wiki/Global_variable)。
答案 2 :(得分:0)
您可以在函数之外声明它:
playerChoice = ''
def humanChoice():
希望有所帮助