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

How to get a function to insert in to a dict? Am i reading the question correctly? I am confused. 2 days already.

hi guys i have been at this for 2 days non stop. and i am confused. How do i get the function to insert the argument/string in to the dictionary? Am I going at the challenge the right way? Am i even supposed to create a dict before hand? how do i get it to count my words? so confuse. Help..... thanks you guys. This is what i have so far.

def word_count('New York is'):
  word_dict={}
  low='New York is'.lower()
  sep=low.split()
  for word in sep:
    word=key:value
      print word

Just to let you guys know, I have been plowing through treehouse videos numerous times, python help files and youtube videos to see if I can understand what I am doing wrong and I just cant see what to do next. I guess the good thing is I am strengthening my foundation.

your question is confusing, maybe you could also show what are you expecting the function to return . do you want the function to return a dictionary? or number of words in your sentence?

Hi pouyazadrafiei,

Over in the right column in the "related content" section there is a link to this code challenge. There you can find the complete instructions.

3 Answers

Ricky Catron
Ricky Catron
13,023 Points

First you are setting the parameters wrong. You are trying to use the string 'New York is' you need to use a variable such as myString instead.

Second you are not adding anything into your word_dict. You can add a new key and value to a dictionary by doing something like:

word_dict["KeyName"] = 1

You can change it the same way:

word_dict["KeyName"] += 1

Once you are finished with the dictionary return it instead of printing anything:

def myFunction():
    return word_dict

Hi Ricky. Thanks for your explanation. i see where i went wrong. Thanks for also pointing out the (word_dict["KeyName"] += 1) I couldn't figure out the value part of the challenge.

Ricky Catron
Ricky Catron
13,023 Points

Of Course glad to help out. Dictionarys can be tricky not to throw to much at you at once but check out named tuples they are similar and very cool.

Thanks Ricky i will take a look at that soon enough. i am very interested in any pointers that can improve my skills. I am at the moment looking at the teachers with classes challenge. I find that I have to follow a certain structure when creating an answer for the challenge. for example having a list or dictionary defined at the top of the code before the loop. Would you have any advice that could help with python?

Ricky Catron
Ricky Catron
13,023 Points

Your solution to those challenges sounds very similar to mine. The certain structure you are starting to see will eventually turn into your own style of coding. Everyone comes up with different solutions to the same problem, overtime they adapt them to work better and eventually end up with a specific style of code which works best for them.

My advice with Python is keep pushing where you can go with it, try a little bit of everything, and learn the industry best practices.

http://newcoder.io/ is a fantastic intermediate site to start pushing yourself past basic scripts and into full applications

https://docs.djangoproject.com/en/1.7/intro/tutorial01/ the official django tutorial is a great intro to backend programming with python

http://zetcode.com/gui/pyqt4/ is a great guide to creating beautiful desktop apps with python

http://kivy.org/#home is an emerging framework for making android and iphone apps with python.

The language is so powerful and far reaching you can do almost anything with it. Pick a project, build it, watch it break, cry a little, fix the bugs, hit your head on the desk when they break it worse, finally fix it, cheer!, show it to a friend and watch them be amazed at what you can do. Goodluck.

Hi Ricky thank you very much for the advice. I well appreciate it. Your guidance can help my choose my path as a python programmer. I will have a look at the links soon. Thank you once again. Have a great day. Rey