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)
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)
x + y = z to z = x + y.