定义变量后出现变量未定义错误

问题描述 投票:0回答:1
def multiply():

    surrounding_x()
    
    multiplied = False
    x_index = chars.index("x")
    before_x = chars[x_index - 1]
    after_x = chars[x_index + 1]
    num = 2
    while num <= 50:
        if int(before_x) ==  num:
            print("X is being multiplied by " + str(num) + ".")
            multiplied = True
            return multiplied

            break
        else:
            num += 1 

    if num  == 50:
        print('X is not being multiplied. ')
Here is where I defined the variable multiplied as being True.

def order():
    
    division()
    addition()
    subtraction()
    multiply()

    if multiplied == True:
        pass
    if division == True:
        pass
    if addition == True:
        pass
    if subtraction == True:
        pass

In my editor, it told me that the variable multiplied wasn't defined. All of the other variables (division, addition, and subtraction) have similar if not identical syntax defining them. 

我尝试重命名变量。我想也许我犯了拼写错误。我不知道是什么原因造成的,所以如果有人可以提供帮助那就太好了。作为一名初学者程序员,我认为问题的答案可能很简单,但我还不明白。谢谢!

python variables syntax
1个回答
0
投票

您需要在两个函数中将变量声明为全局变量。像这样:

def func1():
    global var1

def func2():
    global var1
© www.soinside.com 2019 - 2024. All rights reserved.