我的变量不会被定义为python

时间:2014-04-21 01:35:43

标签: python variables

它会出现错误"全球名称' yourTeam'未定义"。感谢

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]

2 个答案:

答案 0 :(得分:2)

正如上面的其他人已经提到过的,你真的应该去阅读一两个python教程。

我还建议阅读“PEP 20 Zen of Python”并了解编写pythonic代码的含义。

虽然在说这个问题时,我认为SE在与新程序员打交道时过于激进。请参阅下面的 个人 ,了解您要实现的目标。 。

#!/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()

答案 1 :(得分:0)

您不能随意将变量添加到其他函数中,除非您将其传入,或者它是全局的。

>>> 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
>>> 

这会导致错误,因为oof中未定义fo()。改变它的一种方法是添加global个变量:

>>> def foo():
...     global oof
...     oof = 0
... 
>>> def fo():
...     global oof
...     oof+=1
... 
>>> foo()
>>> oof
0
>>> fo()
>>> oof
1
>>> 

您还可以通过传入变量来修复错误:

>>> def foo():
...     oof = 0
...     return oof
... 
>>> def fo(oof):
...     oof+=1
...     return oof
... 
>>> oof = foo()
>>> oof
0
>>> oof = fo(oof)
>>> oof
1
>>> 
您的代码中的

Global个变量:

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]

在代码中传入变量:

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]