+ 1

Please why is this code not working?

def definex(a): x=a definex(2) print(x)

29th Mar 2018, 5:09 PM
MrCubeLord Bx4
MrCubeLord Bx4 - avatar
3 Answers
+ 6
The variable "x" is only accessible in the function definex() and cannot be used outside it, i.e. It has a local scope. To use it outside the function, you will have to give it a global scope. Corrected Code - def definex(a): global x x = a definex(2) print(x) Notice the second line, it declares a Global scope to the variable"x" . so we can use it anywhere
29th Mar 2018, 5:19 PM
Nikhil
Nikhil - avatar
+ 2
print within function or return x and print(definex(2))
29th Mar 2018, 5:18 PM
Markus Kaleton
Markus Kaleton - avatar
+ 1
okk
29th Mar 2018, 5:18 PM
MrCubeLord Bx4
MrCubeLord Bx4 - avatar