After a bit of digging, I found the issue. It has to do with the way imported variables are stored and updated. What I would guess is happening, is that the variable is just a number, so it is stored as a “constant”, which means the only way to change it is to completely reassign the value. So when you change the variable in game_logic.py
it replaces the old one with a new number and the one used in main.py
stays the same. (that wasn’t a very good explanation but I’m not sure how to explain it really)
Example of the problem:
main.py
from otherfile import my_var, changeMyVar, getMyVar
print(my_var, getMyVar())
changeMyVar(7)
print(my_var, getMyVar())
otherfile.py
my_var = 10
def changeMyVar(x):
global my_var
my_var = x
def getMyVar():
return my_var
Now if you run main.py, the output should look like this.
10 10
10 7
You can see that the variable used in main.py
doesn’t update, and therefore has the wrong value.
Solution
To solve this problem, just create a getter function inside of game_logic.py
that returns the scores as they should be.
add to game_logic.py
def get_scores():
return user_score, computer_score
use in main.py
user_score, computer_score = get_scores()
One other thing I would recommend is instead of doing from game_logic import *
, only import what you will actually use, like this from game_logic import game_logic, get_scores
and then inside of your loop you call this to get the scores user_score, computer_score = get_scores()
.
There are other ways to do it, but if the scores are just global variables that are just numbers then this is how you should do it.