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
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3f9fb-2c4b8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3f9fb-2c4b8.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?