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 trialJohn Smith
Python Web Development Techdegree Student 1,052 PointsI don't understand this challenge. Can someone please help?
I don't understand the concept of packing and unpacking even with examples and seeing other peoples code and answers to this challenge, I'm not understanding it enough. I'm a bit frustrated because nothing is working. I don't even know what to write because to me, even the question of the what the challenge is asking, doesn't make any sense. I don't just want the answer, I want a in depth look at how all the moving parts are working within the code. I do not completely see how packing and unpacking can be helpful in someway when writing code. If someone knows of any special or extra resources to learn this concept it would be much appreciated.
"I need you to make a function named word_count. It should accept a single argument which will be a string. The function needs to return a dictionary. The keys in the dictionary will be each of the words in the string, lowercased. The values will be how many times that particular word appears in the string."
Check the comments below for an example.
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.
def word_count(string)
dictionary = {}
return dictionary(string).lower
3 Answers
Grigorij Schleifer
10,365 PointsHi Ruddy, it is absolutely normal to feel frustrated. Just donΒ΄t give up and you will get better.
I don't think you need to unpack or pack anything for this challenge. Read this post carefully since it explains the concept of packing/unpacking pretty well.
And I will comment out the challenge so you can follow along:
# the method will take a string that consists of different words ...
def word_count(arg):
# you should lower case the string and split it into separate words
# split will create a list with separated words and you can loop over every single one
# and count how many identical words are in the list
split = arg.lower().split()
# you correctly created a dictionary to store words and their counts
dictionary = {}
# loop over the list of separated words
for key in split:
# every word is a key for your dictionary and split.count()
# will count how many times a special word is encountered in the list
dictionary[key] = split.count(key)
return dictionary
Does it makes sense?
Yuyang Peng
5,665 PointsWOW Just have to THANK YOU for what you did there. Makes so much sense and the post is awesome! Really helpful to python beginners. thank you so much :)
John Smith
Python Web Development Techdegree Student 1,052 PointsThank you so much Grigorij for taking the time to explain all that in much more detail. I really appreciate it. I think the reason why I felt frustrated is because I want to grasp the concept right away, but learning programming languages can be quite challenging sometimes for beginners. I will continue studying this concept in order to improve my understanding of it. Thank you.
Lazarus Mbofana
10,941 Pointsthe method will take a string that consists of different words ...
def word_count(arg):
you should lower case the string and split it into separate words
split will create a list with separated words and you can loop over every single one
and count how many identical words are in the list
split = arg.lower().split()
you correctly created a dictionary to store words and their counts
dictionary = {}
loop over the list of separated words
for key in split:
every word is a key for your dictionary and split.count()
will count how many times a special word is encountered in the list
dictionary[key] = split.count(key)
return dictionary
worked perfectly fine.
Tatiane Lima
6,738 Pointsi spent about 2 hours to solve this challenge and i wrote twice code. π°
Grigorij Schleifer
10,365 PointsGrigorij Schleifer
10,365 PointsHey guys, it is my pleasure to help. I am a beginner myself and struggle all the time. But the journey is the goal and not the goal itself.