-2

It gives the error "global name 'yourTeam' is not defined". Thanks

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()
class tbhl:
    def __init__(self):
        self.teamList["Mustangs", "Wolves", "Indians", "Tigers", "Bears", "Eagles", yourTeam]
class game:
    def __init__(self, homeScore, awayScore):
        #games left in a team class
        pass
startup() #<-The line that the error points to
tbhl = tbhl()
print tbhl.teamList[7]
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
kannoli
  • 328
  • 2
  • 5
  • 13

2 Answers2

2

As others above have already mentioned you really should go and read a python tutorial or two.

I would also recommend reading the "PEP 20 Zen of Python" and learn what it means to write pythonic code.

Though in saying that, I for one believe that SE is overly aggressive when dealing with new programmers. Please see below for my personal take on what you are trying to achieve. .

#!/usr/bin/env python


class TextBasedHockeyLeague(object):    
    def __init__(self):
        """
        Initialises a new instance of the TextBasedHockeyLeague object. 
        Also initialises two instance variables, _team_list (collection of teams) and
        _your_team (placeholder for your team name.)
        """
        super(TextBasedHockeyLeague, self).__init__()
        self._team_list = ["Mustangs", "Wolves", "Indians", "Tigers", "Bears", "Eagles"]
        self._your_team = None

    def game_startup(self):
        """
        Entry function for your TB Game.
        Asks user for a team name, and adds that team name into the _team_list collection.
        """
        print "Welcome to the Text Based Hockey League!"
        print "You will be tasked with being a team's General Manager"
        print "Please enter your teams name:"
        self._your_team = raw_input()        
        self._team_list.append(self._your_team)        

    def print_team_list(self):
        """
        Possible extension point for printing a friendlier team list.
        """
        print self._team_list


# see [http://stackoverflow.com/a/419185/99240][2] for an explanation of what
# if __name__ == '__main__': does.
if __name__ == '__main__':
    league = TextBasedHockeyLeague()
    league.game_startup() 
    league.print_team_list()
Jeremy Cade
  • 1,351
  • 2
  • 17
  • 28
0

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]
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76