0

[solved]what i have to do to fix the error of this code?

it gives an UnboundlocalError, i cant understand what is the mistake i have done. num1 = int(input()) opr = input() numx = int(input()) oprx = input() def operator_check() : if str(opr) in opr_list : return opr_list[str(opr)] else : pass def add() : if oprx == "=" : numx += num1 return numx elif str(oprx) == "+" : numx += num1 return operator_check() else : pass def sub() : if oprx == "=" : numx -= num1 return numx elif oprx == "-" : numx -= num1 return operator_check() else : pass def mult() : if oprx == "=" : numx *= num1 return numx elif oprx == "*" : numx *= num1 return operator_check() else : pass def div() : if oprx == "=" : numx /= num1 return numx elif oprx == "/" : numx /= num1 return operator_check() else : pass def factorial(num1) : if num1 == 1 : return 1 else : return num1*factorial(num1-1) def expo() : if oprx == "=" : numx **= num1 elif oprx == "**" : numx **= num1 return operator_check() else : pass def sqrt() : if numx == "sqrt" : num1 = num1 ** (1/2) return num1 opr_list = {"+": add(),"-": sub(),"*": mult(),"/": div(),"!": factorial(num1),"**": expo(),"sqrt": sqrt()} operator_check()

16th Jul 2020, 6:48 PM
Ayash Sameer
Ayash Sameer - avatar
9 Answers
+ 3
You need to add a line `global numx` Before reassigning value to the variable <numx> in each function. def add() : if oprx == "=" : global numx # line added numx += num1 return numx elif str(oprx) == "+" : numx += num1 return operator_check() else : pass Similarly you need to add that line before reassigning in each function. Python does not have variables declaration like in other languages so it has to figure out the scope of a variable itself. So it follows a simple rule, if there is an assignment to the variable inside a function, that variable is local to that function. So what is happening that when you write `numx += num1` It creates <numx> locally and tries to read its value before it is even assigned. This results in an UnboundLocalError.
16th Jul 2020, 7:34 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 2
Thanks both of you ! 😊 I totally forgot about that local and global thing
16th Jul 2020, 7:22 PM
Ayash Sameer
Ayash Sameer - avatar
+ 1
Ayash Sameer What type of error are you getting?
16th Jul 2020, 6:53 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
num1 = 5 opr = * numx = 2 What is oprx? Ayash Sameer
16th Jul 2020, 6:58 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
try to put the result into another variable rather than overwriting the numx variable by making calculations on global variables. Example: def add(): result = numx + num1 return result another way is to pass them into a function and then return or print them. Example def add(x, y): x += y return x print(add(4,5)) Result: 9 This will be a much more clear program. By the way you are not printing anything with this program and it makes it very hard to test it. And as an answer to your question when you are using the numx += num1 statement the function thinks you are trying to create a new local variable. You can read further from here : https://stackoverflow.com/questions/10851906/JUMP_LINK__&&__python__&&__JUMP_LINK-3-unboundlocalerror-local-variable-referenced-before-assignment
16th Jul 2020, 7:50 PM
Arda Atıcı
Arda Atıcı - avatar
+ 1
The Unboundlocalerror: local variable referenced before assignment is raised when you try to use a variable before it has been assigned in the local context. Python doesn't have variable declarations , so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local . To solve this problem, you can explicitly say it's a global by putting global declaration in you function. The global statement does not have to be at the beginning of the function definition, but that is where it is usually placed. Wherever it is placed, the global declaration makes a variable to global variable everywhere in the function. http://net-informations.com/python/err/local.htm
26th Oct 2020, 5:05 AM
berkninan
0
I will share the link to above code here, so you can get a better understand https://code.sololearn.com/cwgY3Pe8PVy8/# sample input : num1 = 3 opr = + numx = 4 oprx = =
16th Jul 2020, 6:53 PM
Ayash Sameer
Ayash Sameer - avatar
0
its an unbound local error, it says the variable "numx" is referenced before assignment
16th Jul 2020, 7:00 PM
Ayash Sameer
Ayash Sameer - avatar