How does the python3 global variable record the final value of the change?

the program structure is similar to the following

import sys
...
-sharp
sls=0

def search(keyword):
    

if __name__ == "__main__":
    result=["1","2","3"]
    for f_word in result:
        s_result = search(f_word)
    
    print("\n",":",len(result),"",":",sls)

at present, how does this sls accumulate in the method and finally display correctly? no, solve, thank you!

Sep.10,2021

if modified in the method, declare global sls at the beginning of the function body as a global variable. The
or search method returns True or False , which is modified in the for loop.


< H2 > Code < / H2 >
import sys
...
-sharp
sls=0

def search(keyword):
    if keyword == "1":
        global sls
        sls +=1

    print("" , keyword)

if __name__ == "__main__":
    result=['1','2','3','1']
    for f_word in result:
        s_result = search(f_word)
    
    print('\n',':',len(result),'',':',sls)
< H2 > execute < / H2 >
 1
 2
 3
 1

 : 4  : 2
Menu