Search code examples
pythonimportbooleanpython-multithreading

Updating a boolean variable from an imported file


I'm new to programming and have run into a problem. An imported function, bonnie_movement, is supposed to stop running when the variable game_end is True, now when I try to update that variable (which was also imported) in the file where it has been imported to it doesn't update it. Example:

import os 
import random 
import threading 
import time 
from movement_testing import * 

def p(): 
    global game_end 
    a = input("> ") 
    if a == "end": 
        game_end = True 
        time.sleep(0.5) 
        print(str(game_end)) 

threading.Thread(target=p).start() 
threading.Thread(target=bonnie_movement).start() 

The print you see there though gives out True but bonnie_movement just keeps on going. Does anyone know what I can do?

Thanks in advance!

Edit: The variable a has nothing to do with the question, it servers no real purpose except stopping the function on declaring game_end = True immediately. The print is also as a way to see if game_end is updated in the function p and serve's no real purpose.


Solution

  • I posted the same question on reddit and here's the answer:

    Globals in python are global to modules, not across all modules. The line you claim imports the variable actually doesn't.

    If 'game_end' is a property of 'Movement_testing' then try accessing it like

    movement_testing.game_end = True

    and

    print(str(movement_testing.game_end))