[英]How to use variables in multiple different functions in Python 3
I define a variable named 'computerChoice' in a function and then attempt to use it in another variable but it says that 'computerChoice' is undefined... Being new to Python, I am not sure why this is happening so I hope that you could help answer my question! 我在一个函数中定义了一个名为'computerChoice'的变量,然后尝试在另一个变量中使用它,但是它说'computerChoice'是未定义的...作为Python的新手,我不确定为什么会这样,所以希望您可以帮助回答我的问题!
def computerDecision():
import random
for x in range(1):
num = random.randint(1,4)
if num == 1:
computerChoice = 'rock'
elif num == 2:
computerChoice = 'paper'
elif num == 3:
computerChoice = 'scissors'
def determineWinner():
if userInput == computerChoice:
print("You both chose " + computerChoice)
print("DRAW!")
elif userInput == 'rock' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'rock' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print('USER WINS!')
elif userInput == 'paper' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("USER WINS!")
elif userInput == 'paper' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("USER WINS!")
The computerChoice
variable is restricted in scope to the function in which it was defined. computerChoice
变量的范围仅限于定义它的功能。 This is to prevent functions from interfering with one another. 这是为了防止功能相互干扰。 You can declare it to be global (accessible from anywhere) with the keyword global
: 您可以使用关键字global
声明它是全局的(可从任何地方访问):
global computerChoice
This is probably not what you want, however, as your functions do not need to interact with each other. 但是,这可能不是您想要的,因为您的功能不需要相互交互。 You can just make computerDecision()
return its choice. 您可以使computerDecision()
返回其选择。
import random
def computerDecision():
for x in range(1):
num = random.randint(1,4)
if num == 1:
return 'rock'
elif num == 2:
return 'paper'
elif num == 3:
return 'scissors'
def determineWinner():
computerChoice = computerDecision()
if userInput == computerChoice:
print("You both chose " + computerChoice)
print("DRAW!")
elif userInput == 'rock' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'rock' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print('USER WINS!')
elif userInput == 'paper' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("USER WINS!")
elif userInput == 'paper' and computerChoice == 'scissors':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'rock':
print("The computer chose " + computerChoice)
print("COMPUTER WINS!")
elif userInput == 'scissors' and computerChoice == 'paper':
print("The computer chose " + computerChoice)
print("USER WINS!")
Also note that computerDecision()
can be redefined more simply, as just return random.choice(("rock", "paper", "scissors"))
. 还要注意的是computerDecision()
可以更简单地重新界定,刚刚return random.choice(("rock", "paper", "scissors"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.