Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tor Ivan Boine
4,057 PointsThe code works, but I don't know how. teachers.py 2 of 5
I tried out this code on my computer
x = {'key' : 'value', 'key2' : 'value2', 'key3': ['lol', 'hehe', 'jaja', 'haha'], 'key4': 'tata'}
def num_teachers(x):
y = len(x.keys())
return y
def num_courses(dict1):
total = 0
for value in dict1.values():
for items in value:
total += 1
return total
it returns 19. and it is correct for the assignment. shouldn't it return 7? it counts the value of 'key' as 5, value of 'key2' as 6, and the value of 'key3' as 4. I'm confused.
4 Answers
andren
28,558 PointsThe code essentially just adds the length of the value it processes to the total variable, in other words the function does the same as this code:
def num_courses(dict1):
total = 0
for value in dict1.values():
total += len(value)
return total
So the code does not count the number of values, it counts the length of the values.
Since the value of key is a string it returns the length of said string ("value" is made up of 5 characters), the same happens with key2 and key4. Since key3 contains a list you get the length of the list which is the number of elements in the list.
If you modified your test dictionary to only pass in lists (which is what the challenge does) like this:
x = {'key' : ['value'], 'key2' : ['value2'], 'key3': ['lol', 'hehe', 'jaja', 'haha'], 'key4': ['tata']}
Then you would get 7 as you expect.
Tor Ivan Boine
4,057 Pointssame with the 3rd task. the question
For this step, make another new function named courses that will, again, take the dictionary of teachers and courses. courses, though, should return a single list of all of the available courses in the dictionary. No teachers, just course names!
x = {'key' : 'value', 'key2' : 'value2', 'key3': ['lol', 'hehe', 'jaja', 'haha'], 'key4': 'tata'}
def courses(x):
total = []
for value in x.values():
for items in value:
total.append(items)
return total
this returns:
['v', 'a', 'l', 'u', 'e', 'v', 'a', 'l', 'u', 'e', '2', 'lol', 'hehe', 'jaja', 'haha', 't', 'a', 't', 'a']
and is correct. how? :)
Tor Ivan Boine
4,057 Pointsyes I know that 'hello' returns 5 and ['hello'] returns 1. that isn't the question.
the question is how is
x = {'key' : 'value', 'key2' : 'value2', 'key3': ['lol', 'hehe', 'jaja', 'haha'], 'key4': 'tata'}
correct when it returns 19?
edit: ok, I see that the sample dict the task uses only have values in lists. That's why the code works. But the code fails if not all values are in lists ...
andren
28,558 PointsThe dictionary you illustrated will cause an incorrect result, but it is incorrect because you are using incorrect values. If you look at the dictionary that the challenge itself passes to your function all of the values are lists of strings, it never passes in a value that is simply a string by itself. The code is designed to work on lists and only lists.
Meaning that this is not a valid dictionary to pass into the function {"key1: "hello"} but this is {"key1: ["hello"]}. Most functions are only designed to work on a specific type of value and passing in a different one will of course lead to wrong results. In a real application you might add some value validation code that returned an error when an incorrect value type was passed in, but in a scenario where you know for sure how the values passed in will be structured that is not strictly needed.
So the challenge is marked as valid because the functions are only tested with dictionaries that the challenge checker passes in (where the values are always lists), the type of dictionaries you have written where raw strings are used are not tested as that is not a part of this challenge.
If that still did not answer your question then I think your question needs some clarification because I've really tried to clarify what is happening in this challenge and in your code. Your confusion seems to stem from somewhere completely different from where I originally thought.
Tor Ivan Boine
4,057 Pointsyeah sorry. My brain can be slow sometimes. Your explanations were on point.
Tor Ivan Boine
4,057 PointsTor Ivan Boine
4,057 Pointsbut the code worked in the assignment. the code will return this
teachers = {'mike' : 'java', 'donald' : 'c++', 'robert' : ['ruby', 'python']}as 9. even though there are only 4 courses. What am I missing here?
andren
28,558 Pointsandren
28,558 PointsIt works because in the assignment the values are always lists, not strings. In the code you just posted
mikeanddonaldjust contain a string, whilerobertis a list. Even if you just assign a single course it still need to be inside a list.Take this code:
What do you think it will print? Well for a string the
lenfunction counts the number of characters, so 5 will be printed. Now what about this code:Now you are not asking about the length of the a string, but a list containing a single string. The
lenfunction counts the number of objects when called on a list so in this case it would return 1.As mentioned above your code essentially just counts the length of the value, which means that when you use a value that is a string it will count the length of the string, when you pass it a list it will count the number of items in the list.
If you change your code so that the string you had assigned to
mikeanddonaldis within a list like this:teachers = {'mike' : ['java'], 'donald' : ['c++'], 'robert' : ['ruby', 'python']}Then you would get the expected result.
Edit:
Maybe breaking down your loop further will make it easier to understand . Your loop breaks each value it is passed into individual parts. When you loop over a list you get the individual items, so with a list like this:
["hello"]the "hello" string would be pulled out and you would increase thetotalby 1 and then the loop would be done. If"hello"was looped though then each individual letter would be pulled out so first "h" is pulled andtotalis increased by 1, theneis pulled out andtotalis increased by 1 again, this continues until the loop has gone though all of the letters andtotalwould the equal 5.That's why
key1produces 5 andkey26 and so on, that is a count of how many letters are in the string.