global variable not recognized; says its local variable
Question:
Its not recognizing the global variable. I experienced this issue before and thought the global variable would prevent the error but no matter what i try it always returns this: local variable ‘P1o’ referenced before assignment
#import pwinput
import PySimpleGUI as sg
P1o = ("")
P2o = ("")
MAX_ROWS = MAX_COL = 10
def Battleship1():
layout = [
[sg.Text('Player1 please enter your ship positions'), sg.InputText('', size=(10,1), key='input_P1o')],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('player1 values', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Submit':
P1o = P1o.append(int(values['input_P1o']))
window.close()
elif event == 'cancel':
window.close()
break
layout = [
[sg.Text('Player2 please enter your ship positions'), sg.InputText('', size=(10,1), key='input_P2o')],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('player2 values', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Submit':
P1o = P1o.append(int(values['input_P1o']))
turn()
turn_()
if event == 'cancel':
window.close()
break
""" i set up the multiplayer function"""
layout = [ [sg.Text("Welcome to battleship!!nObject of the game is to sink all five of your enemy's ships!!!nlabel your ship positions with a number (0-9)n and another number (0-9)n and separate each position with spaces e.g 1,2 2,3 3,4")],
[sg.Button('Single Player'), sg.Button('Multiplayer')] ]
window = sg.Window('Menu', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Multiplayer':
window.close()
Battleship1()
break
Answers:
I would suggest you to use P1o
and P2o
as your local
variables, as they are only needed in your battleship
function.
You can use them as your global
or as your local
variables. I’ll leave the choice to you.
CASE 1: Using it as local
variable:
def Battleship1():
P1o = ""
P2o = ""
Here, it just creates two local
variables for the function and would prevent the referenced before assignment error.
CASE 2: Using it as global
variable:
def Battleship1():
global P1o, P2o
global
keyword is used to reference the globals()
dictionary where all the global
variables are mentioned. So, it would tell the function that P1o
and P2o
is a global
variable.
Its not recognizing the global variable. I experienced this issue before and thought the global variable would prevent the error but no matter what i try it always returns this: local variable ‘P1o’ referenced before assignment
#import pwinput
import PySimpleGUI as sg
P1o = ("")
P2o = ("")
MAX_ROWS = MAX_COL = 10
def Battleship1():
layout = [
[sg.Text('Player1 please enter your ship positions'), sg.InputText('', size=(10,1), key='input_P1o')],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('player1 values', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Submit':
P1o = P1o.append(int(values['input_P1o']))
window.close()
elif event == 'cancel':
window.close()
break
layout = [
[sg.Text('Player2 please enter your ship positions'), sg.InputText('', size=(10,1), key='input_P2o')],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window('player2 values', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Submit':
P1o = P1o.append(int(values['input_P1o']))
turn()
turn_()
if event == 'cancel':
window.close()
break
""" i set up the multiplayer function"""
layout = [ [sg.Text("Welcome to battleship!!nObject of the game is to sink all five of your enemy's ships!!!nlabel your ship positions with a number (0-9)n and another number (0-9)n and separate each position with spaces e.g 1,2 2,3 3,4")],
[sg.Button('Single Player'), sg.Button('Multiplayer')] ]
window = sg.Window('Menu', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'Multiplayer':
window.close()
Battleship1()
break
I would suggest you to use P1o
and P2o
as your local
variables, as they are only needed in your battleship
function.
You can use them as your global
or as your local
variables. I’ll leave the choice to you.
CASE 1: Using it as local
variable:
def Battleship1():
P1o = ""
P2o = ""
Here, it just creates two local
variables for the function and would prevent the referenced before assignment error.
CASE 2: Using it as global
variable:
def Battleship1():
global P1o, P2o
global
keyword is used to reference the globals()
dictionary where all the global
variables are mentioned. So, it would tell the function that P1o
and P2o
is a global
variable.