-3

I am working on a project and keep getting variable not defined errors when I run the script. How can I solve this without declaring global variables

def func1():
    x = 1
def func2():
    y=5
    x + y = z
    print(z)
0

1 Answer 1

1

x is in the local scope of func1, thus it can not be read from func2. You can use global x = 1 but I would not recommend it. Instead pass x to func2:

def func1():
    x = 1
    return x
def func2(x):
    y = 5
    z = x + y # You had this backwards as well (i.e. x + y = z)
    print(z)
x = func1()
func2(x)
Sign up to request clarification or add additional context in comments.

2 Comments

You'll also need to change x + y = z to z = x + y.
Didn't even notice thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.