Def in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Def in Python

Well i have a simple question with (def) code in Python and maybe i already know the answer.. but i need a explanation! Why when i create a def in Python with one variable inside and i try to print this variable outside the def, the program show error?? https://code.sololearn.com/ci62143vyF0y/?ref=app

27th Aug 2019, 4:33 AM
Gabriel
3 Answers
+ 2
All variables declared inside functions will by default be deleted, when the function call ends. This can be prevented by declaring the variables global. https://code.sololearn.com/cxgyYwjY22Kp/?ref=app
27th Aug 2019, 6:26 AM
Seb TheS
Seb TheS - avatar
+ 2
Scope of 'var' is within function example1(). If you want to print the value of var', u have to return the value from example1(), capture the value, then print it.
27th Aug 2019, 5:20 AM
Kuri
Kuri - avatar
+ 1
ty Seb TheS and Kuri for the answers, you help me a lot!
27th Aug 2019, 7:57 PM
Gabriel