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 trialNiko McCarty
4,559 PointsDo not split only on spaces error...
For some reason, my code is not passing for this challenge and I keep getting an error which says "Do not split only on spaces." I cannot continue on in the course until I pass this challenge, unfortunately. Any advice?
# 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):
string = string.lower()
new_string = string.split(' ')
final = {}
for key in new_string:
if key in final:
final[key] += 1
else:
final[key] = 1
return final
2 Answers
Umesh Ravji
42,386 PointsYou would have to remove the line:
new_string = string.split(' ')
and replace it with
new_string = re.split('\s', string)
or you can also can split without any arguments to get the same result [Edit]: not longer seems to work..
new_string = string.split()
Umesh Ravji
42,386 PointsYou have to split on whitespace.
new_string = re.split('\s', string)
Niko McCarty
4,559 PointsAdding this did not work for me.
def word_count(string): string = string.lower() new_string = string.split(' ') new_string = re.split('\s', string) final = {} for key in new_string: if key in final: final[key] += 1 else: final[key] = 1 return final