-1

I am creating a game that is played in the terminal and I wanted to reduce the amount of repetitive code by using functions eg

def p1_end_turn(p1_turn, p2_turn):
    p1_turn = False
    p2_turn= True
    return p1_turn, p2_turn

however when running the program with the code p1_end_turn(p1_turn, p2_turn) the variable p1_turn and p2_turn have not been changed. How can I make this code properly return the variables (im using python3.9.1 and have checked that the function works properly)

Platinum
  • 23
  • 5
  • 1
    Just returning a variable doesn't change the original. You need to actually _assign_ that returned value. – Charles Duffy Oct 29 '21 at 20:21
  • 7
    If you have variables outside of the function that you want changed, you'd have to set them explicitly to the result of the function (like `a, b = func(a, b)`), or use globals, or use a class to hold the variables and functions. Changing variables' values in a function doesn't change the originals. – Random Davis Oct 29 '21 at 20:21
  • You don't return variables, you return objects. Are you actually trying to rebind the names in the calling scope? Cause that's not a good use for a function. What's the context of this? This may be an [XY problem](https://meta.stackexchange.com/q/66377/343832), especially considering that the parameters are unused. BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Oct 29 '21 at 20:22
  • Does this answer your question? [How can I return two values from a function in Python?](https://stackoverflow.com/questions/9752958/how-can-i-return-two-values-from-a-function-in-python) – Woodford Oct 29 '21 at 20:25
  • @Woodford No, OP's already doing that. – wjandrea Oct 29 '21 at 20:27
  • @wjandrea Clearly not as OP isn't assigning the result of the function to anything as described in the answers to the linked question. – Woodford Oct 29 '21 at 20:30
  • i don't know if it sounds stupid or not, but instead of direct variable define a class that store variable and implement a singleton patteren, so whenever function call then you can change those point value in that class instance – sahasrara62 Oct 29 '21 at 20:32
  • @Woodford Ah, I see what you mean, but that'd be a different question, like "How do I capture multiple return values from a function?" – wjandrea Oct 29 '21 at 20:33
  • @sahasrara62 That's a good idea to use a class, but why a singleton? – wjandrea Oct 29 '21 at 20:34
  • @wjandrea i am not that good with design pattern, but with singleton, OP can use same instance of class and can change values whenever that class instance is called and sincle there will be only one, then it won't be much difficult to maintain different class instance of same class. this pattern has own disadvantage and OP might need to refactor all code – sahasrara62 Oct 29 '21 at 20:36

3 Answers3

4

Returning a variable doesn't change the value of the variables. I think reading this page about variable scope and this page about immutable and mutable variables would be helpful.
As it has mentioned in second link, boolean variables are immutable objects.
If you really want to stick to your current code (which i highly recommend to first read links and decide what to do!), you can use something like this to change turns:

def change_trun(p1_turn, p2_turn):
    return not p1_turn, not p2_turn

# some codes
p1_turn, p2_turn = change_turn(p1_turn, p2_turn)

Last line is the key. variables should update with returned values.

  • Thank you very much this helped me fix my code and using the links provided I was able to better understand functions (an area I didnt fully grasp till now). – Platinum Oct 30 '21 at 12:03
0

Have a look at variable scopes (and possibly at mutable/immutable objects). In short you only change p1_turn and p2_turn inside the function. This means that outside the function the values stay the same.

Einliterflasche
  • 473
  • 6
  • 18
  • This is helpful info, but doesn't really answer the question. How would you use scope or objects to accomplish the desired result? – wjandrea Oct 29 '21 at 20:26
  • 1
    If you wanted to change `mutable objects` (lists, dicts, sets, custom classes) your code would work just fine. However for `immutable objects` (strings, ints, ...) I would suggest using @Random Davis' solution. – Einliterflasche Oct 29 '21 at 20:31
-1

Your variables in the function are not the same as the ones out of it. Therefore you have to store the output in the variables you want.

def p1_end_turn(p1_turn, p2_turn):
    p1_turn = False
    p2_turn= True
    return p1_turn, p2_turn

P1_turn = True
P2_turn = True

P1_turn, P2_turn = p1_end_turn(P1_turn, P2_turn)
Klaus
  • 315
  • 1
  • 10