I want to increment the global variable when i call a function but its giving me an error can some body help me out | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to increment the global variable when i call a function but its giving me an error can some body help me out

x=0 def main(): print(x) x+=1 def check(): for I in range(10): main() check() expected output: 0 1 2 3 4 5 6 7 8 9 10 actual outpu: UnboundError: Local variable referenced before assignment

7th Oct 2018, 4:46 PM
Velivela Sai Chinna Madhu Babu
Velivela Sai Chinna Madhu Babu - avatar
3 Answers
+ 6
add 'global x' after 'def main():'
7th Oct 2018, 5:50 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 4
you have to use global specifier for use global vars inside functions
7th Oct 2018, 4:50 PM
KrOW
KrOW - avatar
0
thank you very much it's worked
7th Oct 2018, 6:13 PM
Velivela Sai Chinna Madhu Babu
Velivela Sai Chinna Madhu Babu - avatar