Short description of the scoping rules
What exactly are the Python scoping rules?
Python – change function’s variable inside another function via argument
I want to find how to change a variable via a function that isn’t the one that the variable initiated in using the other function’s argument (to make it viable for many usages).
why print(num) print ‘0’? num is global variable and I changed num to ‘6’ in local function
“It’s my code. When I compiled this code, I expected ‘print(num)’ to print ‘6’. The reason behind my expectation is that, firstly, the variable ‘num’ is global. Thus, in the first line, ‘num’ was reset to ‘0’. Additionally, in the fourth line, ‘print(average(7, 5, 9))’, the function ‘average’ was defined. As the code was executed by the last line, ‘num’ was changed from 7 to 6. Therefore, ‘print(num)’ should have printed ‘6’. However, it actually printed ‘0’. Why did this situation occur? And what do I need to change to have ‘6’ printed as ‘num’, referencing ‘num1’?”