gpt4 book ai didi

python - 正确地将变量传递给函数

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:26 25 4
gpt4 key购买 nike

我创建了一个剪刀石头布应用程序,允许用户指定他们想要玩的回合数。在这些指定的回合之后,应用程序将显示分数。在确保应用程序正常运行后,我尝试为应用程序的特定部分创建函数。我试图返回变量以传递给其他函数,但应用程序不起作用。我想我在这里遗漏了一些关键的东西。

我试图弄乱选项卡并更改某些变量,但我没有运气。

当前错误信息:

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)

最佳答案

你的 main 中没有 rounds 所以它会导致错误。您正在从 roundChoice() 返回 rounds 但未为其分配任何变量。我已经分配了它并通过了 runGame() 以获得预期的行为。您的主要实现需要:

def main():
welcomeMsg()
rounds=roundChoice()
runGame(rounds)
gameResults(score, computerscore)

关于python - 正确地将变量传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58792297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com