[Tutor] Dictionary Issue

Alan Gauld alan.gauld at btinternet.com
Thu Aug 6 20:30:07 CEST 2015

On 06/08/15 18:17, Ltc Hotspot wrote:

> View revised code here:
> fname = raw_input("Enter file name: ")
> handle = open (fname, 'r')
> c = Counter(['address'])
> count = dict ()
> maxval = None
> maxkee = None
> for kee, val in count.items():
>          maxval = val
>          maxkee = kee
> for line in handle:
>      if line.startswith("From: "):
>          address = line.split()[1]
>          count[address]= count.get(address,0) +1
> print maxkee and maxval

Let's hold up here a second. You do understand that
Python executes your code from top to bottom, yes?

So reading your code from the top you have a loop that
sets maxval and maxkee before you even put anything
into count. How do you think that would ever work?

You have a lot of people spending time trying to
help you here, but you do need to exercise a little
bit of insight yourself. That code can never work
and it has nothing to do with Pyhon it is your
logic that is faulty.

Try working through it with a pencil and paper.
Write down what each variable contains at each
stage of the program. (or just print it to see).

You have been very close to a solution but you
seem to be getting farther away rather than closer
which suggests you are trying stuff without
understanding why.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list