为什么要将变量指定给“变量”&引用;? 所以我在树屋网站的Python课程的中间,问题是这样问的:
创建一个名为most_classes的函数,该函数使用教师词典。每个键都是老师的名字,它们的值是他们所教课程的列表。大多数课程应返回课程最多的老师 在这里,我在下面发布了我从树屋论坛上的资源中找到的正确代码,我问了同样的问题,但没有得到答复-那么指派教师“”到底做什么?我很困惑为什么要将变量指定给“变量”&引用;? 所以我在树屋网站的Python课程的中间,问题是这样问的:,python,variables,python-3.x,python-3.4,Python,Variables,Python 3.x,Python 3.4,创建一个名为most_classes的函数,该函数使用教师词典。每个键都是老师的名字,它们的值是他们所教课程的列表。大多数课程应返回课程最多的老师 在这里,我在下面发布了我从树屋论坛上的资源中找到的正确代码,我问了同样的问题,但没有得到答复-那么指派教师“”到底做什么?我很困惑 # The dictionary will be something like: # {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Tec
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
# 'Kenneth Love': ['Python Basics', 'Python Collections']}
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
def most_classes(my_dict):
count = 0
teacher = "" #this is where I am confused!
for key in my_dict:
if(len(my_dict[key]) > count):
count = len(my_dict[key])
teacher = key
return teacher
它将默认值分配给教师,该值将被代码中的实际教师姓名替换。
teacher=”“
确保如果my_dict
为空,则在未设置teacher=key
的情况下,您不会退出for循环。否则,如果my_dict
为空,teacher
将在未设置的情况下返回
如果注释掉该行,则按如下方式调用函数:
most_类({})
您将得到以下结果(因为teacher
在返回之前从未初始化):
UnboundLocalError:作业前引用的局部变量'teacher'
为什么不删除该行并测试它
def most_classes(my_dict):
count = 0
teacher = "" # this is where I am confused!
for key in my_dict:
if(len(my_dict[key]) > count):
count = len(my_dict[key])
teacher = key
return teacher
def most_classes_cavalier(my_dict):
count = 0
for key in my_dict:
if(len(my_dict[key]) > count):
count = len(my_dict[key])
teacher = key
return teacher
if __name__ == "__main__":
dic = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
'Kenneth Love': ['Python Basics', 'Python Collections']}
print "The teacher with most classes is - " + most_classes(dic)
print "The teacher with most classes is - " + most_classes_cavalier(dic)
dic = {}
print "The teacher with most classes is - " + most_classes(dic)
print "The teacher with most classes is - " + most_classes_cavalier(dic)
这是我运行程序时得到的-
The teacher with most classes is - Jason Seifer
The teacher with most classes is - Jason Seifer
The teacher with most classes is -
Traceback (most recent call last):
File "experiment.py", line 30, in <module>
print "The teacher with most classes is - " + most_classes_cavalier(dic)
File "experiment.py", line 20, in most_classes_cavalier
return teacher
UnboundLocalError: local variable 'teacher' referenced before assignment
上课最多的老师是-Jason Seifer
上课最多的老师是杰森·塞弗
上课最多的老师是-
回溯(最近一次呼叫最后一次):
文件“experience.py”,第30行,在
打印“班级最多的老师是-”+班级最多的骑士(dic)
文件“experience.py”,第20行,在大多数职业中
归国教师
UnboundLocalError:分配前引用了局部变量'teacher'
我看到@martijn pieters已经提供了解释,但在本例中,Python解释器会更快地为您提供解释。
teacher=“”
将teacher
绑定到空字符串。当my_dict
为空时,这是一个很好的默认值(例如,循环永远不会迭代,并且没有其他值分配给teacher
)。似乎只为max(my_dict,key=lambda x:len(my_dict[x])
@TigerhawkT3:你的意思是max(my_dict,key=lambda x:len(my_dict[x]),默认值=)
如果没有老师,默认值将起作用,但如果字典中有没有课的老师,则不起作用。@MartijnPieters,TigerhawkT3你们处于另一个层次,我甚至不理解你们发布的代码。英雄联盟