[Tutor] Dictionary Issue

Ltc Hotspot ltc.hotspot at gmail.com
Fri Aug 7 02:15:56 CEST 2015


Question1: How type of argument should I use for dict, i.e.,user argument
or list argument.

Read captured traceback:

TypeError
Traceback (most recent call last)
C:\Users\vm\Desktop\apps\docs\Python\assignment_9_4_26.py in <module>()
      1 fname = raw_input("Enter file name: ")
      2 handle = open (fname, 'r')
----> 3 count = dict.keys()
      4 for line in handle:
      5     if line.startswith("From: "):

TypeError: descriptor 'keys' of 'dict' object needs an argument

In [99]:

Question2: Are all the loop failures resolved in the revised code?

Revised code is available at
https://gist.github.com/ltc-hotspot/00fa77ca9b40c0a77170

Regards,
Hal

On Thu, Aug 6, 2015 at 4:20 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 07/08/15 00:11, Ltc Hotspot wrote:
>
>> Questions(1):Why does print line, prints blank space; and, (2) print
>> address prints a single email address:
>>
>
> See my previous emails.
> You are not storing your addresses so address only holds the last address
> in the file.
> line is at the end of the file so is empty.,
>
> In [72]: print count
>> {'gopal.ramasammycook at gmail.com <mailto:gopal.ramasammycook at gmail.com>':
>> 1, 'louis at media.berkeley.edu <mailto:louis at media.berkeley.edu>': 3,
>> 'cwen at iupui.
>> edu': 5, 'antranig at caret.cam.ac.uk <mailto:antranig at caret.cam.ac.uk>':
>> 1, 'rjlowe at iupui.edu <mailto:rjlowe at iupui.edu>': 2, 'gsilver at umich.ed
>> u': 3, 'david.horwitz at uct.ac.za <mailto:david.horwitz at uct.ac.za>': 4, '
>> wagnermr at iupui.edu <mailto:wagnermr at iupui.edu>': 1, 'zqian at umich.edu
>> <mailto:zqian at umich.edu>':
>>  4, 'stephen.marquard at uct.ac.za <mailto:stephen.marquard at uct.ac.za>': 2,
>> 'ray at media.berkeley.edu <mailto:ray at media.berkeley.edu>': 1}
>>
>> Question(3): why did the elements print count('keys') and print
>> count('items') fail?
>>
>
> Because, as shown above, count is a dictionary.
> So items and keys are methods not strings to be passed
> to a non-existent count() function.
>
> So you need, for example:
>
> print count.keys()
>
> Traceback (most recent call last)
>> <ipython-input-76-35c8707b256e> in <module>()
>> ----> 1 print count('items')
>>
>> TypeError: 'dict' object is not callable
>>
>>
> Which is what the error is also telling you.
> You cannot call - ie use () - with a dictionary like count.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>


More information about the Tutor mailing list