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

Python Python Collections (Retired) Dictionaries Word Count

reza sajadian
reza sajadian
718 Points

I need help with my quiz

My quiz is :

Create a function named word_count() that takes a string. Return a dictionary with each word in the string as the key and the number of times it appears as the value.

and what I have created is:

def word_count(): stn_my_str = my_str.lower() mylist = stn_my_str.split() replist = mylist[:] # Keeps the original listed string untouched for word in replist: while True: replist1 = replist[:] if word in replist1: replist1.remove(word) count += 1 continue else: break word_dict['word'] = count count = 0 break

3 Answers

Bart Bruneel
Bart Bruneel
27,212 Points

Hello reza,

It is a bit difficult to see how your code works because it is not formatted right, but I'll give this a shot. Remember you can format the code by typing it inside python . Check the markdown cheatsheet for extra help. Or you can take a snapshot of your workspace if you are coding the challenge in workspaces. Couple of things you could fix.

  • The function word_count should take a string as argument. For the moment no arguments are put into the function. You can fix this like this.
def word_count(string):
  • The lowercasing and the splitting of the string is done right.
  • Then you create a list instead of a dictionary. You can create a dictionary as follows:
my_dict= {}
  • Then you should loop over the words in the list and put them inside the dictionary. Use a for-loop.
for word in mylist:
  • If the word is already inside of the dictionary as a key, we should add one to it's value.
if word in mydict:
 mydict[word]+=1

otherwise

else:
 mydict[word]=1

In the end the dictionary should be returned.

Happy coding!

reza sajadian
reza sajadian
718 Points

Sorry, the previous message was not formatted right. Here is what I have written: Hope it helps to understand my main problem.

string = 'life is life and my life is a good life' my_dict = {}

def word_count():

stn_string = string.lower() mylist = stn_string.split() replist = mylist[:] # replist(as of replicated list) Keeps the original listed string intact for word in replist: while True: if word in replist: replist.remove(word) word_dict[word] += 1 continue else: break break return(word_dict)

Bart Bruneel
Bart Bruneel
27,212 Points

Hey Reza,

There is no need to specify a string of your own. When checking your code, the challenge will pass a string to the function. Specifying your own string will probably give problems. Also remember to pass an argument to the function (see my previous post). Also, the my_dict needs to be specified inside of the function. Two other remarks:

  • it is not needed to make a replica of the list and to remove the words from the replica as you loop over the words. You can just loop over the words in the original list (mylist).

  • There is also no need to add a while-loop. For the moment your code will work as follows:

    • It will go into the for-loop.
    • it will go into the while-loop.
    • if your word is in the list it will remove the word from the list and add 1 to the value of the word in word_dict. Here it assumes that the word is already in the word_dict, but it isn't. So the code will give an error. No need to add a while-loop thus.