Python Forum
Issues with storing variables outside of a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with storing variables outside of a function
#1
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!
Reply
#2
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}")
Reply
#3
you need to return any variable from the function if you want to use it outside of the function
Reply
#4
Thanks for the replies! Is there a specific reason why "player1_mark" is defined outside the function, but "player2_mark" isn't?
Reply
#5
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?
Reply
#6
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
Reply
#7
Globals really should be avoided, as they lead to code that's hard to follow and maintain. Return values from functions instead.
Reply
#8
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
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 1,003 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Storing whole functions in variables dedesssse 3 362 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  Do I have to pass 85 variables to function? Milfredo 10 1,358 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 2,210 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 846 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Temporarily storing the value of a function Men 6 1,094 Jun-21-2020, 06:43 PM
Last Post: Men
  Where to put the global keyword when assigning variables outside a function? new_to_python 8 1,202 Feb-09-2020, 02:05 PM
Last Post: new_to_python
  making a function that writes variables (is possible?) miker2808 3 961 Jan-30-2020, 06:27 PM
Last Post: buran
  I am trying to swap two variables with a Function.... Jeff_Waldrop 4 1,378 Mar-04-2019, 10:19 AM
Last Post: Jeff_Waldrop
  I need help with loop one function and creating variables don 3 1,377 Jan-24-2019, 07:31 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020