岩石剪刀,当我运行它时程序说决定在分配之前被引用,尽管用户选择和计算机选择显示正常,我已经坚持了很长一段时间并且不熟悉编程。
import random
def main():
userinput = int(input("First just enter 0 for rock, 1 for paper, or 2 for scissors! "))
computernumber = random.randint(0,2)
while userinput != 0 and userinput != 1 and userinput != 2:
userinput = int(input("Enter 0 for rock, 1 for paper, or 2 for scissors! "))
print ("You chose ",userchoice(userinput),)
print ("Computer chose ",computerchoice(computernumber),)
print (decision(userchoice,computerchoice))
def userchoice(userinput):
if userinput == 0:
userchoice = "rock";
elif userinput == 1:
userchoice = "paper";
elif userinput == 2:
userchoice = "scissors";
return userchoice
def computerchoice(computernumber):
if computernumber == 0:
computerchoice = "rock";
elif computernumber == 1:
computerchoice = "paper";
elif computernumber == 2:
computerchoice = "scissors";
return computerchoice
def decision(userchoice,computerchoice):
if (userchoice == computerchoice):
decision = "It's a draw!";
elif (userchoice == "rock"):
if(computerchoice == "paper"):
decision = "Computer wins!";
elif (userchoice == "paper"):
if(computerchoice == "rock"):
decision = "You win!";
else:
decision = "Computer wins!";
elif (userchoice == "scissors"):
if(computerchoice == "rock"):
decision = "Computer wins!";
else:
decision = "You win!";
return decision
main()
输出:
first just enter 0 for rock, 1 for paper, or 2 for scissors! 1
You chose paper
Computer chose rock
Traceback (most recent call last):
File "MorerockpaperscissorsEM.py", line 60, in <module>
main()
File "MorerockpaperscissorsEM.py", line 17, in main
print(decision(userchoice,computerchoice))
File "MorerockpaperscissorsEM.py", line 57, in decision
return decision
UnboundLocalError: local variable 'decision' referenced before assignment
答案 0 :(得分:1)
在此行中调用“决定”功能时
print (decision(userchoice,computerchoice))
您将函数'userchoice'和'computerchoice'作为参数传递。调用此函数的正确方法是:
print (decision(userchoice(userinput),computerchoice(computernumber)))