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
srikanth soma
1,572 PointsConvert string to dictionary?
Alright, this one might be a bit challenging but you've been doing great so far, so I'm sure you can manage it. 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.
For the following above task this is what I'm thinking I don't know whether I'm in right direction or not.
- First created new function with one argument
- convert that argument to lower case
- created new dictionary
- Iteration through argument and split it by space I used string.split(" ") I suppose this should split by space but for some reason it splits by each letter in word.
- and I thought once I split that by space then add that to dict that I created(dict = {}) and I've no Idea how to count those words occurence.
Can someone help me whether my thought process is atleast in right direction or how bad I'm thinking this?
def word_count(arg):
string = arg.lower()
dict = {}
for words in string:
word_split_by_space = words.split(' ')
print(word_split_by_space)
word_count("Hello World")
rainmaker
14,690 Pointsrainmaker
14,690 PointsTry not to use python keywords as variable names.. i.e: you are using string and dict which are python keywords you never added the words from your for loop to the dictionary nor did you count the amount of times the word appears in the string.