【问题标题】:How to use variables in multiple different functions in Python 3如何在 Python 3 中的多个不同函数中使用变量
【发布时间】:2019-07-20 16:35:57
【问题描述】:

我在一个函数中定义了一个名为“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!")

【问题讨论】:

  • 你的意思是你试图在另一个函数中使用computerChoice?除非您将变量声明为全局变量,否则这是行不通的
  • 首先修复示例代码中的缩进,因为这一切都搞砸了,然后粘贴一个完整的最小可运行示例,以及运行代码后的完整输出。请记住,帮助您的人也需要能够完全按照您的方式运行您的代码。
  • 修复代码中的缩进,因为不清楚您实际在做什么。其次,根据您运行程序的方式,您有两种选择:使用 global 关键字,或者,从一个函数返回值并将其传递给另一个函数(这是更可取的)

标签: python python-3.x function variables


【解决方案1】:

computerChoice 变量在 scope 中被限制为定义它的函数。这是为了防止功能相互干扰。您可以使用关键字global 将其声明为全局(可从任何地方访问):

global computerChoice

但是,这可能不是您想要的,因为您的函数不需要相互交互。你可以让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!")

另请注意,computerDecision() 可以更简单地重新定义,就像 return random.choice(("rock", "paper", "scissors"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2021-12-31
    • 2019-02-11
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多