我的变量在Python中无法定义

-2 投票
2 回答
666 浏览
提问于 2025-04-18 03:32

它给出了一个错误提示:“全局名称'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 个回答

2

正如上面其他人提到的,你真的应该去看看几个Python的教程。

我还建议你阅读一下“PEP 20 Python之禅”,了解一下什么是写出优雅的Python代码。

不过,我个人觉得,Stack Exchange在对待新程序员时有点过于严格。下面是我对你想要实现的目标的个人看法。

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

这样会出错,因为在fo()函数里没有定义oof。解决这个问题的一种方法是使用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]

撰写回答