Dictionaries and Loops in Python. Using Two Nested loops

Hey there, I’m currently on Dictionaries and Loops in Python. At 7:11 of the video we are given a script “Using Two Nested Loops”

I want to understand this but I’m given an error when I try the code myself.
TypeError: ‘>’ not supported between instances of ‘int’ and ‘dict’

name = input('Enter file:')
handle = open(name)

counts = dict("")
for line in handle:
    words = line.split()
    for word in words:
        counts[word] = counts.get(word,0) + 1

bigcount = None
bigword = None
for word,count in counts.items():
    if bigcount is None or count > bigcount:
        bigword = words
        bigcount = counts

print(bigword, bigcount)

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Thank you! I will do this next time, first time posting with code.

Hello there,

Are you sure this line is correct?:

bigcount = counts
1 Like


What happens if you use len(bigcount)?:

1 Like

Hey All, I figured it out.

it was simple. I had
counts = dict("")

and not
counts = dict()

now the file counts the words in a text document just fine. Thanks for you help :slight_smile: