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

Lloyd Stevens
Lloyd Stevens
10,934 Points

Bummer! word_count() takes 0 positional arguments but 1 was given

This code works for me on my computer but won't let me through when checking the work, it gives me this error.

I have had two attempts at this (rejigging slightly as can be seen with attached code in an attempt to get it to work)

Previously I had the following code but this gave me a message stating that it wasn't counting all the words so I moved it around a bit to see if that would help.

'''python

sentence = ("I am that I am") sentence=sentence.lower() sentence = sentence.split() print (sentence)

def word_count(sentencearg): my_dict={} for i in range(len(sentencearg)): if sentencearg[i] in my_dict: my_dict[sentencearg[i]]=my_dict[sentencearg[i]]+1 else: my_dict[sentencearg[i]]=1 return(my_dict)

print(word_count(sentence)) '''

word_count.py
def word_count():
    sentence = ("I am that I am")
    sentence=sentence.lower()
    sentence = sentence.split()
    my_dict={}
    for i in range(len(sentence)):
        if sentence[i] in my_dict:
            my_dict[sentence[i]]=my_dict[sentence[i]]+1
        else:
            my_dict[sentence[i]]=1
    return(my_dict)

print(word_count())

As far as I can see both work to count the number of times a word is within a sentence?

both print the following to screen for me

{'am': 2, 'i': 2, 'that': 1}

2 Answers

Steven Parker
Steven Parker
231,236 Points

:point_right: Your code may "work", but it's not what the challenge asked for.

The challenge asked you to: "Create a function named word_count() that takes a string." By "takes a string" they mean "takes a string argument". So when the checker tests your answer, it passes a string to your function, but since your function is defined to take no arguments, the error message you see is generated.

But the overall function of your code is very close, so if you just modify it to accept the sentence as an argument instead of creating it internally, you should have no problem passing the challenge.

FYI: When quoting code, use three accents (``` — AKA "backticks"), not apostrophes (''')

Lloyd Stevens
Lloyd Stevens
10,934 Points

Thank you, I can see that now :)