简体   繁体   English

如何在Python 3中的多个不同函数中使用变量

[英]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.

相关问题 了解如何在Python中使用多个线程与Queue调用不同的函数? - Understanding how to use Multiple threads calling different functions with Queue in python? 如何在XSLT中使用python函数和变量? - How to use python functions and variables inside XSLT? Python 如何使用np.select运行不同的函数来修改全局变量? - Python How to use np.select to run different functions to modify global variables? 在python(jupyter)中复制变量/对相同的变量使用不同的函数 - Copy a variable in python (jupyter) / Use different functions with same variables 如何访问 tkinter 中不同 class 的变量以用于不同的功能? - How to access variables from different class in tkinter to use in different functions? 如何使用 class 来存储在不同函数中更新的变量,然后在不同的函数中调用这些更新的变量? - How to use a class to store variables that are updated in different functions, and then call these updated variables in different functions? 如何在不同功能中使用一个列表,Python - How to use one list in different functions, Python 如何在Python的for循环中使用不同的变量 - How to use different variables in a for loop in Python Python多个文件变量和函数 - Python multiple files variables and functions 如何通过不同的函数正确传递变量 - How to properly pass variables through different functions PYTHON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM