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


Messages In This Thread
Issues with storing variables outside of a function - by cerulean747 - Apr-29-2020, 11:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print variables in function? samuelbachorik 3 915 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  User-defined function to reset variables? Mark17 3 1,655 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Storing variables into one file for use in multiple Jupyter notebooks devansing 1 1,745 Feb-05-2022, 10:04 AM
Last Post: ibreeden
  Conjugate Gradient having issues with defining A (function to solve [A]{x} = {b} ) DimosG 2 2,833 Sep-21-2021, 08:32 PM
Last Post: 1968Edwards
  Storing whole functions in variables dedesssse 3 2,096 Jul-29-2021, 09:17 PM
Last Post: deanhystad
  Do I have to pass 85 variables to function? Milfredo 10 4,319 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 6,014 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 2,494 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Temporarily storing the value of a function Men 6 2,816 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 3,044 Feb-09-2020, 02:05 PM
Last Post: new_to_python

Forum Jump:

User Panel Messages

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