我创建了一个石头剪刀布应用程序,该应用程序允许用户指定他们想要玩的回合数。在指定的回合之后,应用程序将显示分数。确保应用程序正常运行后,我尝试为应用程序的特定部分创建功能。我试图返回变量以传递给其他函数,但是该应用程序不起作用。我想我这里缺少一些关键的东西。
我试图弄乱标签并更改某些变量,但我没有运气。
当前错误消息:
Traceback (most recent call last):
File "C:/Users/iamep/Desktop/Python Projects/RockPaperScissors.py", line 96, in <module>
main()
File "C:/Users/iamep/Desktop/Python Projects/RockPaperScissors.py", line 90, in main
runGame(rounds)
NameError: name 'rounds' is not defined
代码:
#display Welcome Message
def welcomeMsg():
print ("Welcome to Rock Paper Scissors")
#User Inputs Number of Rounds
def roundChoice():
user_input = input('Enter Number of Rounds: ')
try:
rounds = int(user_input)
except ValueError:
print('Not a number')
return rounds
def runGame(rounds):
import random #imports a random module for the computer.
game = ["ROCK", "PAPER", "SCISSORS"] #sets the game answers.
count = 0 #game count is set to zero.
score = 0 #player score is set to zero.
computerscore =0 #computers score is set to zero.
while count == 0: #starts game if count is zero.
for i in range (rounds): #repeats the turns to the user specified rounds
answer = input ("Pick rock, paper or scissors: ") #users input their choice
print (answer.upper()) #prints the users answer
computer= random.choice(game) #computer picks randomly
print ("Computer picks",computer) #Display Computer Option
if answer.upper() == "ROCK" and computer == "ROCK": #This whole set of code sets the game that what beats what.
print ("Its a tie!") # prints after each response that who won.
count +=1 #the count variable is increased.
elif answer.upper() == "PAPER" and computer == "PAPER":
print ("Its a tie!")
count +=1
elif answer.upper() == "SCISSORS" and computer == "SCISSORS":
print ("Its a tie!")
count +=1
elif answer.upper() == "PAPER" and computer == "ROCK":
print ("You win!")
count +=1
score +=1 #If User Wins Score is Added
elif answer.upper() == "PAPER" and computer == "SCISSORS":
print ("You lose!")
count +=1
computerscore +=1 #IF Computer Wins computerscore is added
elif answer.upper() == "ROCK" and computer == "PAPER":
print ("You lose!")
count +=1
computerscore +=1
elif answer.upper() == "ROCK" and computer == "SCISSORS":
print ("You win!")
count +=1
score +=1
elif answer.upper() == "SCISSORS" and computer == "ROCK":
print ("lose!")
count +=1
computerscore +=1
elif answer.upper() == "SCISSORS" and computer == "PAPER":
print ("You win!")
count +=1
score +=1
return score, computerscore
def gameResults(score, computerscore):
if score < computerscore: #Display round results
print ("Your score is", score)
print ("Computers score is",computerscore)
print ("Computer wins!.")
if score > computerscore:
print ("Your score is", score)
print ("Computers socre is",computerscore)
print ("You win!.")
if score == computerscore:
print ("Your score is", score)
print ("Computers socre is",computerscore)
print ("Its a tie!!.")
def main():
welcomeMsg()
roundChoice()
runGame(rounds)
gameResults(score, computerscore)
您的主机中没有rounds
,因此会导致错误。您正在从rounds
返回roundChoice()
,但未为其分配任何变量。我已经分配了它,并通过了runGame()
以获取预期的行为。您的main
实现必须为:
def main():
welcomeMsg()
rounds=roundChoice()
runGame(rounds)
gameResults(score, computerscore)