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
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])
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.
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.