Relative Content

Tag Archive for pythonscope

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’?”