Python: Unable to use variable from another file even though the file is imported

I am trying to write a game of rock, paper, scissors, and I am getting an error about a file I imported not having an attribute of “quitGame“, even though I should have that variable imported along with the others.

Here is the function that has the quitGame variable (uneeded parts filtered out):

def playRps():
    game = "".join([playerAction, computerAction])
    global quitGame

    outcomes = {
        "rr": tied,
        "pp": tied,
        "ss": tied,
        "rp": playerLoss,
        "ps": playerLoss,
        "sr": playerLoss,
        "rs": playerWin,
        "pr": playerWin,
        "sp": playerWin,

    if playerAction == "q":
        quitGame = True  # TODO: Figure out why this isn't working in the file
        action = outcomes.get(game)
        if action:
            print("Invalid input!")

You can also find the entire file here.

Here is the file that should runs the program (uneeded parts filtered out):

import functions  # Imports the file
while True:
    playGame = str(input('Would you like to play "Rock, Paper, Scissors"? (Y/n): '))

    if playGame == "y":
        while True:
            if functions.quitGame == True:

    elif playGame == "n":
        print("Terminating program...")

        print("Unknown input. Please enter a valid answer.")

You can also find the entire file here.

Here is the interaction that gave me the error I am trying to fix:

(.venv) [email protected]:rock_paper_scissors$ /home/johnny/Projects/rock_paper_scissors/.venv/bin/python /home/johnny/Projects/rock_paper_scissors/
Would you like to play "Rock, Paper, Scissors"? (Y/n): y

    Rock, paper, or scissors?
    Acceptable responses are...
    "r": Chooses rock.
    "p": Chooses paper.
    "s": Chooses scissors.

    "q": Quits the game.
Tied! No points awarded.
Traceback (most recent call last):
  File "/home/johnny/Projects/rock_paper_scissors/", line 18, in <module>
    if functions.quitGame == True:
AttributeError: module 'functions' has no attribute 'quitGame'

I find this very odd, because other variables such as playerScore and computerScore work as expected. I have functions. added before each variable and function, so it makes no sense as to why it’s telling me I don’t have it.

I even added global quitGame in the function that creates the variable. I don’t see why this isn’t working.

My question to you guys is:

  • Why am I getting an error when I call that variable? What am I doing wrong?


You have to use global before the variables is “requested” not “declared”.

So if you want quitGame to be global you have to declare it were do you prefer (for example in and then were do you want to use it (as the example in a function) use “global quitGame”.

You can tell to your function to access to a global var called quiteGame, instead of declaring quiteGame as global.

Leave a Reply

Your email address will not be published. Required fields are marked *