Dict() is not callable

I’ve been stopped while trying to do the exercise for the dictionaries section of Scientific Computing with Python. I follow the exercise video while working at my own pace, and I’m quickly able to verify that the code is not the issue. I run Spyder on Python 3.7, and up to this point, every bit of code I put in came out fine. Evidence:


fname = input('Enter File: ')
if len(fname) <1 : 
    fname = 'clown.txt'
hand = open(fname)
di = dict()
line 12, in <module>
    di = dict()

TypeError: 'dict' object is not callable

di = dict() alone shouldn’t cause errors. Error says that’s 12th line, what other code is before pasted part?

1 Like

That’s wrong ^^°
To make an empty dictionairy it’s either data=dict() or data={}.

Did you maybe make a dictionairy called “dict”? A common beginner issue is to overwrite Python keywords. Which Python doesn’t mind but ofcourse if you later try to use the keyword, you run into an error.

Because the error suggest dict would be an dict-object, instead of a function CREATING a dict-object.

dict() should create a empty dictionary for di, as that’s it’s function, and the code is identical to that of the exercise video, which I’ve included, with the timestamp of where I had to stop due to this issue.

And, for whoever asked for the full code, I’m posting it here(minus the few lines that spyder puts in containing personal info)

fname = input('Enter File: ')
if len(fname) <1 : 
    fname = 'clown.txt'
hand = open(fname)
di = dict()

for line in hand:
    line = line.rstrip()
    # print(line)
    wds = line.split()
    # print(wds)
    for w in wds:
        if w in di:
            di[w] = di[w] + 1
            print('**Existing**')
        else:
            di[w] = 1
            print('**NEW**')
        print(w,di[w])

This code doesn’t explain the error. Unless dict name is before assigned to something else, that error shouldn’t be happening…

that code is all I’ve written in the editor, at this timestamp in the exercise video 09 - EX - Python for Everybody Course - YouTube (518 seconds after start) shows the exact same code(except there’s one print line added in). I was doing this exercise when this issue happened.

Could you add code below before that line and check what it prints?

print(dict)
print(type(dict))

there is nothing printed except the input block running

I can use this code no problem, without getting any error. Ofcourse I gotta make a random text-file.

Anyway, add a print(dict) in front of the line that causes the error - it should print “<class dict>”.

Input something, otherwise the added lines won’t get executed.

I did, the same error still pops up.

But what added print commands printed?

none of them. only the input command printed, which is not a part of the error.

If I got this correctly:

  • You’ve inputed something when prompt asked.
  • Nothing was printed from the added print commands, before erroring line.
  • Error still occurred.

Something is surely strange around here. Perhaps executed file is different than the one you are changing?

1 Like

No, as the program as a failsafe to go back into a valid file if nothing was inputted. and I still input the file the failsafe goes to anyways.

and, if it isn’t clear about the program’s intention, it’s meant to open up a file, read it, break it up, and count how many times each word is used.

If you add print() statements and they don’t output anything, there is a good chance for whatever reason the interpreter is running another file and not the code you are currently working on.

Restart the entire program, reopen the file and make sure you can add print() and it actually prints something.

so, I put in a print command stating test, and the code suddenly started to function