Dictionaries - code in course comes with an error

Hello, i follow Scientific Computing with Python and in lesson 9, When I write the code which Dr Chuck is using, it comes with an error. Any advice?

purse = dict()
purse[‘money’] = 12
purse[‘crisps’] = 3
purse[‘keys’] = 75

TypeError Traceback (most recent call last)
----> 1 purse = dict()
2 purse[‘money’] = 12
3 purse[‘crisps’] = 3
4 purse[‘keys’] = 75
5 print(purse)

TypeError: ‘dict’ object is not callable

That’s weird, code should work and build-in dict object definitely is a callable.

Where are you trying to run that code? Is that all what is in the run file?

Strange, it should just work like it’s supposed to.
Did you type it yourself or copy it? (sometimes copy/paste result in strange characters, invoking errors). Just to be sure I typed your code and it works in 2 different text editors.

Can you try to call one key in your dictionary, just to see if that works: print(purse["money"])

@sanity i’m using jupyter notebook

@Brain150 no, I wrote the code (not copy and paste), if i run the print(purse[“money”]) it says NameError: name ‘purse’ is not defined

It becomes more weird now…
I just tried it in Jupyter labs and it runs like a charm, so there a few options:

(With the risk of sounding like the much appreciated help-desk guy who needs to ask the obvious questions to rule it out before diving deep into problems: )

  • Try for line 1: purse = {} (creating an empty dictionary). If this works, I don’t know why the original did not, but at least you can move on.
  • If no spelling errors were made in line 1 or line 5, it could be the installation of Jupyter Notebook (I am even less of an expert on this, but it does seem logic if all Python related things have been ruled out), so you could try to repair, update of reinstall Jupyter.

(A more cumbersome method to test the second option would be to install Python on your computer, install a text editor (Atom, Sublime, etc.) and give it another try. If this works, it points to a Jupyter related issue, in which case…yeah, you get it…reinstall, repair or update Jupyter…)

When you use Jupiter NoteBook, you have to keep in mind that you can run any cells individually. This can cause unexpected behavior.
Try to click on the [Kernel] menu and choose the [Restart & Run All]
Probably you did not run any necessary cells before try to run print(purse[“money”])

The quotes you use in your code seem not to be OK!

Try to copy and paste the code below to see wether it runs.

purse = dict()
purse['money'] = 12
purse['crisps'] = 3
purse['keys'] = 75

In jupyter notebook it did not work but as I changed the quotes it worked!

@Dtzz ,
Your code nicely works in Pycharm IDE. So there is no mistake in code.

It worked well for me, I don’t think it’s the quotes, but you should look into it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.