简体   繁体   English

为什么要为“”分配变量?

[英]Why would you assign a variable to “”?

so i'm in the middle of the Python course on the Treehouse website and question asks exactly this: 所以我在Treehouse网站上的Python课程中,问的问题恰好是这样的:

Create a function named most_classes that takes a dictionary of teachers. 创建一个名为most_classes的函数,该函数需要一个教师词典。 Each key is a teacher's name and their value is a list of classes they've taught. 每个键都是老师的名字,其值是他们所教课程的列表。 most_classes should return the teacher with the most classes. most_classes应该返回班级最多的老师。

Here I have posted the correct code below that I have found from a resource on the Treehouse forums and I have asked this same question but got no reply - So what exactly does assigning teacher = "" do? 在这里,我发布了以下正确的代码,这些代码是我在Treehouse论坛上的资源中找到的,并且我也提出了相同的问题,但没有得到答复-那么分配教师=“”到底有什么用? I am so confused 我感到很困惑

 # 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 = "" ensures if my_dict is empty you won't exit the for loop without ever setting teacher = key . teacher = ""确保如果my_dict为空,则在未设置teacher = key情况下不会退出for循环。 Otherwise, if my_dict is empty, teacher will be returned without ever having been set. 否则,如果my_dict为空,则无需设置将返回teacher

If you comment out that line, then call your function like this: 如果注释掉该行,请像下面这样调用函数:

most_classes({})

You'll get this (since teacher never gets initialized before it is returned): 您会得到的(因为teacher在返回之前从未初始化过):

UnboundLocalError: local variable 'teacher' referenced before assignment

Why not remove that line and test it? 为什么不删除该行并对其进行测试?

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)

This is what I get when I run the program - 这是我运行程序时得到的-

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

I see that @martijn-pieters has already provided the explanation, but in this case the Python interpreter would have done that for you much quicker. 我看到@ martijn-pieters已经提供了解释,但是在这种情况下,Python解释器会为您更快地完成该解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM