简体   繁体   中英

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:

Create a function named most_classes that takes a dictionary of teachers. 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.

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? 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 . Otherwise, if my_dict is empty, teacher will be returned without ever having been set.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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