Global definitions are generally capitalized
Global definitions are generally capitalized
APPLE =1
a = None
def fun():
global a
a =2return a +100print(APPLE)print(a)print(fun())print(a)
zbx@zbxpc:~$ /usr/bin/python3 /home/zbx/desktop/mofan_python.py
1
None
1022
If the a in the custom function wants to become a global variable, you can use global, and you also need to define a outside;
Note: the a before print(fun()) is a local variable of the function, and the one printed after running is a global variable.