Python Forum

Full Version: Issues with storing variables outside of a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

Python newbie here. I'm confused as to why after running the below function, when I call one of the variables defined within that function, I receive an error message (that the variable is undefined). My code is below:

    def player_input():
    player1_mark = input("Please pick a marker 'X' or 'O'")
    player2_mark = ''
    if player1_mark=='X':
        player2_mark='O'
    elif player1_mark=='O':
        player2_mark='X'
    print("Player 1 is: " + player1_mark)
    print("Player 2 is: " + player2_mark)
In particular, after defining player1_mark, I get an undefined error when I try to retrieve "player2_mark" outside of the function, even though executing the function successfully prints both variables. Can anyone help out with this? Thanks!
In general variables that are created inside a function are local to the function and cannot be read outside it.

In the smallest of programs, you could create the variable outside the function (making it a global). But this gets to be unwieldy as programs get larger.

Otherwise, your function should return the data you need, and the calling program can assign that data to variables that are in scope for that location. Something like:

def player_input():
    # do some processing that populates player1_mark and player2_mark
    return player1_mark, player2_mark

p1, p2 = player_input()
print(f"player 1 is: {p1}")
print(f"player 2 is: {p2}")
you need to return any variable from the function if you want to use it outside of the function
Thanks for the replies! Is there a specific reason why "player1_mark" is defined outside the function, but "player2_mark" isn't?
The code in your first post isn't valid. The lines in the function (starting at line 2) need to be indented. Right now, I can't tell what lines are part of the function and which ones aren't.

It looks like both player1_mark and player2_mark are defined inside the function. Can you show why you think that they're not?
Use global keyword to make variables global which is defined inside the function

>>> def player_input():
...    global player1_mark
...    player1_mark = input("Please pick a marker 'X' or 'O'")
...
>>> player_input()
Please pick a marker 'X' or 'O'X
>>> print("Player 1 is: " + player1_mark)
Player 1 is: X
Globals really should be avoided, as they lead to code that's hard to follow and maintain. Return values from functions instead.
def some_function(value1, value2):
    ...
    # everything which is assigned in this block
    # is local to this function (some_function)
    # and is deleted, after leaving the context
    # of the function
    # if your function should return something
    # then return it
    result = value1 + value2
    # if not return result, result is deleted
    return result


my_result = some_function(13, 42)
# if the return is missing, then the function return implicit None