You cannot arbitrarily add a variable into a different function unless you are passing it in, or it is global.
>>> def foo():
... oof = 0
...
>>> def fo():
... oof+=1
...
>>> foo()
>>> fo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in fo
UnboundLocalError: local variable 'oof' referenced before assignment
>>>
This causes an error because oof
is not defined in fo()
. One way to change that is to add global
variables:
>>> def foo():
... global oof
... oof = 0
...
>>> def fo():
... global oof
... oof+=1
...
>>> foo()
>>> oof
0
>>> fo()
>>> oof
1
>>>
You can also fix the error by passing in the variable:
>>> def foo():
... oof = 0
... return oof
...
>>> def fo(oof):
... oof+=1
... return oof
...
>>> oof = foo()
>>> oof
0
>>> oof = fo(oof)
>>> oof
1
>>>
Global
variables in your code:
def startup():
print "Welcome to the Text Based Hockey League!"
print "You will be tasked with being a team's General Manager"
global yourTeam
yourTeam = raw_input()
class tbhl:
def __init__(self):
global yourTeam
self.teamList["Mustangs", "Wolves", "Indians", "Tigers", "Bears", "Eagles", yourTeam]
class game:
def __init__(self, homeScore, awayScore):
#games left in a team class
pass
startup()
mylist = tbhl()
print tbhl.teamList[6]
Passing in variables in your code:
def startup():
print "Welcome to the Text Based Hockey League!"
print "You will be tasked with being a team's General Manager"
yourTeam = raw_input()
return yourTeam
class tbhl:
def __init__(self, yourTeam):
self.teamList["Mustangs", "Wolves", "Indians", "Tigers", "Bears", "Eagles", yourTeam]
class game:
def __init__(self, homeScore, awayScore):
#games left in a team class
pass
yourTeam = startup() #<-The line that the error points to
mylist = tbhl(yourTeam)
print tbhl.teamList[6]