Title: Change parameter to a boolean value in python
Tags: python;function;boolean
Question: I was trying to create a back button in pygame using python but my parameter(condition) is not being set to false, how do I do this?
Here is my code:
```def back(condition):  #condition - any boolean variable in my script
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if 5 + 125 &gt; mouse[0] &gt; 5 and 5 + 125 &gt; mouse[1] &gt; 5:
        gameDisplay.blit(backBtn, (5, 5))
        if click[0] == 1:
            if condition:
                condition = False
    else:
        gameDisplay.blit(backBtn_hover, (5, 5))
    
```
If you have any sort of answer please answer.
Thank you! :D

Comment: I wanted condition to be a parameter that takes in a boolean variable and only set condition to false when the back button is clicked

Comment: When the back button is pressed, the condition should be set to false. This is the kind of functionality I want but it the condition is not automaticly being set to false by itself. If its impossible to set the condition to false, I will have to create an individual button for every menu in my game.

Comment: I didn't understand. Do you want to set `condition` to be by default `False` ?

Comment: So what is the problem, what's not working in your example? It sounds very simple.

Comment: use a class with an instance member `condition` (`self.condition`). Give it a better name.

Comment: function parameters are passed by value for simple types like bool and numbers and by *const* reference for strings (non mutable object)


Here is another answer: Since ```condition``` is of an immutable type, any change to it is only visible inside the function.
You will need to return it and assign it back when calling the function.
```def back(condition):  #condition - any boolean variable in my script
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if 5 + 125 &gt; mouse[0] &gt; 5 and 5 + 125 &gt; mouse[1] &gt; 5:
        gameDisplay.blit(backBtn, (5, 5))
        if click[0] == 1:
            if condition:
                condition = False
    else:
        gameDisplay.blit(backBtn_hover, (5, 5))
    return condition

...

condition = back(condition)
```
It is usually not advisable to make it a global variable so I'm not going to suggest that.


Here is another answer: Your answer likely is due to scoping of your variables, three examples provided below that hopefully will lead you to a solution:
Situation 1
```condition = True

def back(condition):
    condition = False
    print(f&quot;{condition=}&quot;)


def main():
    local_condition = back(condition)
    global_condition = condition
    print(f&quot;{local_condition=}\n{global_condition=}&quot;)


if __name__ == '__main__':
    main()
```
Results:
```condition=False
local_condition=None
global_condition=True
```
Situation 2 with added return in back funciton
```condition = True

def back(condition):
    condition = False
    print(f&quot;{condition=}&quot;)
    return condition

def main():
    local_condition = back(condition)
    global_condition = condition
    print(f&quot;{local_condition=}\n{global_condition=}&quot;)


if __name__ == '__main__':
    main()
```
Results 2:
```condition=False
local_condition=False
global_condition=True
```
Situation 3 redefining global
```condition = True

def back(condition):
    condition = False
    print(f&quot;{condition=}&quot;)
    return condition

def main():
    global condition
    condition = back(condition)
    global_condition = condition
    print(f&quot;{condition=}\n{global_condition=}&quot;)


if __name__ == '__main__':
    main()
```
Results 3:
```condition=False
condition=False
global_condition=False
```


Here is another answer: Do you expect this to happen?
```condition = True
back(condition)
print(condition) # False
```
That's not how Python works. You can't change variables passed to functions. Something like that would work.
```container = { 'condition': True }
back(container)
print(container['condition']) # False
```
Because in this case you don't change the variable, but its content. But anyway it's bad code. Normally you would do something like that:
```condition = True
condition = back(condition)
print(condition) # False
```
Of course you need to add a return statement to your function.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:07/25/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><function><boolean>', 'Title': 'Change parameter to a boolean value in python', 'ViewCount': 760, 'id': 63091671, 'user_AboutMe': '<p>Hi, I have a youtube channel caleed IT guys, I would very much appreciate it if you checked it out!\nHere it is:\n<a href="https://www.youtube.com/channel/UCBwe5fGDR6-48RPywCHXvJQ/" rel="nofollow noreferrer">My channel</a></p>\n', 'user_AccountId': 19053416.0, 'user_DisplayName': 'IT_Guys', 'user_Reputation': 27.0, 'user_Views': 12.0, 'user_id': 13911108.0}}