IIUC, you are trying to modify a variable in a function, which is originally defined outside a function. This is a variable scoping problem. Do check this awesome article to get an understanding of how variable scopes work in python.
Back to your code, the issue here is that even though you run the function to modify the variable sets
to 1, python goes back to the outer scope where sets
were set to 0.
sets = 0 #<-- outer scope
def search_str():
... #do something
sets = 1 #<-- inner scope
search_str()
print(sets) #back to outer scope
# 0
Solution 1: Pass as a parameter and return
You will have to pass the variable as a parameter to the function and then return it as follows -
sets = 0
def search_str(sets): #<-- pass as paramter
... #do something
sets = 1 #<-- modify
return sets
output = search_str(sets) #<-- save the output
print(output)
# 1
TIP: If your function is already returning another output, you can actually return, store and unpack multiple return values at once -
- In your return statement, return everything you need
return x, y, sets
- Then, while calling use -
X, Y, sets = search_str(...)
Solution 2: Set scope to global
If passing a parameter and returning is not an option for you, the scope of the variable has to be made global -
sets = 0
def search_str():
global sets #<-- set the scope to global
... #do something
sets = 1
search_str()
print(sets)
# 1
EDIT: As an additional pointer, as Matthias points out in his comments correctly, global scoping is usually avoided as it can cause a lot of problems if not careful.
Here is a great StackOverflow thread detailing Why are global variables evil?