我有一个项目列表。然后我有一个功能,在那里找到一个项目。
def find(self, runners):
for runners in self._runners:
if runners.name == name:
return runners
因此,当我输入runnersworld.find(peter)
时,它应该带回一个打印语句,例如“找到记录或找不到记录。每条记录都有一个runid和一个转轮名称。但是当我运行这个函数时,我得到一个全局变量命名它没有定义。我有两个罚款:
RunnerClass
class Runner(object):
def __init__ (self, runnerid, name):
self._runnerid = runnerid
self._name = name
@property
def runnerid(self):
return self._runnerid
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
def get_fee(self, basicfee, moneyraised):
raise NotImplementedError("AbstractMethod")
def __str__(self):
rep = "Runner: " + self._runnerid + " " + self._name + "\n"
return rep
俱乐部课程
class Club (object):
def __init__(self, clubid, name):
self._clubid = clubid
self._name = name
self._runners = []
@property
def clubid(self):
return self._clubid
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@property
def runners (self):
return self._runners
@runners.setter
def runners(self, runners):
self._runners = runners
def add(self, runners):
if runners not in self._runners:
self._runners.append(runners)
def remove(self, runners):
if runners in self._runners:
self._runners.remove(runners)
def find(self, runners):
for runners in self._runners:
if runners.name == name:
return runners
def __str__(self):
rep = "Club: " + self._clubid + " " + self._name + "\n"
for runner in self._runners:
rep += str(runner)
return rep
我已将我的查找功能编辑为:
def find(self, name):
for runners in self._runners:
if runners.name == name:
print("Runner found in Records")
else:
print("Runner not found in record")
它似乎有效,但当我尝试运行runnersworld.find(peter)
和runners4life.find(peter)
时,我得到了:
记录中找不到跑步者
记录中找不到跑步者
记录中找不到跑步者
记录中找不到跑步者
记录中找不到跑步者
记录中找不到跑步者
但是在runners4life中肯定是一个彼此
答案 0 :(得分:5)
在下面的代码中:
def find(self, runners):
for runners in self._runners:
if runners.name == name:
return runners
没有定义变量name
。你可能意味着
def find(self, name):
for runners in self._runners:
if runners.name == name:
return runners