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 (2016, retired 2019) Dictionaries Word Count

Jon Rolfsen
Jon Rolfsen
3,243 Points

Following all criteria, Program works Everywhere but TreeHouse...

I've accounted for possible punctuation, all spaces, and caps. Difference in tabs is accounted for. Either python version is accounted for. Tested multiple texts.

The only thing I can figure out is that there are bugs/limits with the Treehouse compiler?

Tested Using

  • repl.it
  • ST3
  • IDLE
  • Notebooks
  • Python 2&3
wordcount.py
"""Following all criteria, program works everywhere but Treehouse"""

def word_count(string):
    string = "".join(c for c in string if c not in ('!','.',',','?',':',';')) # Removes Punctuation
    wList = string.lower().split(" ")   # lowercase, No-Spaces, List
    d = {w:0 for w in wList}            # Creates Dictionary of empyy vals from List
    for w in wList:                     # For every word in the List
        if w in d.keys():                   # If the word is found in the Dictionary
            d[w] += 1                           # Add 1 to the val associated with that Word
    return d                            # Return the Dictionary after all is done

# Code I used for testing
words = """Lorem ipsum dolor sit amet! Adipisicing elit, sed do eiusmod tempor; incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"""
print(word_count(words))

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

it's not a bug. the error tells you to split on all whitespace, but you are only splitting on the space character. there are other whitespace characters such as tab. use split with no argument to split on all whitespace.

Jon Rolfsen
Jon Rolfsen
3,243 Points

Thank you very much James. This is the type of question trips me up too often. It would be a welcome add if they showed what the test input was

Jon Rolfsen
Jon Rolfsen
3,243 Points

I think this is what I ended up doing:

def wordCount(string): string = "".join(c for c in string if c not in ('!','.',',','?',':',';')) words = string.lower().split() d = {w:words.count(w) for w in words} return d