Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dictionaries Word Count

reza sajadian
reza sajadian
718 Points

Can anybody help me with my quiz?

This is my quiz:

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 this is my answer:

my_dict = {}

def word_count(string):

  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:
      if word in my_dict:
        my_dict[word] += 1
      else:
        my_dict[word] = 1
  break
return my_dict

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Reza;

Take a look at the great answer Tree Casiano provided on this question in this post.

Hope it helps and happy coding,
Ken

reza sajadian
reza sajadian
718 Points

Thanks Ken. It was completely helpful. thumbs up.

Ken Alger
Ken Alger
Treehouse Teacher

Pleased it helped and welcome to Treehouse!

:thumbsup: