TypeErrors

Benjamin Kaplan benjamin.kaplan at case.edu
Sat Feb 28 20:10:24 CET 2009


On Sat, Feb 28, 2009 at 12:24 PM, Sean Novick <daddysean23 at yahoo.com> wrote:

> I'm having issues with a phone-number database.
> Here is a code snippet:
>  def menu():
>
>         print '''Please make a selection from the following options:
>         To add an Employee - enter: 1
>
>         For a complete listing of Employees - enter: 2
>         To see the 'test page' and exit the database - enter: 3'''
>         print
>         return input(":")
> #Start of Program
>     foo = phonedb()
>     loop = 1
>     choice = 0
>     while loop == 1:
>         choice = menu()
>         if choice == 1:
>             print "Enter the Employee's full name: "
>             n = raw_input(':')
>             print "Enter the Employee's full telephone number: "
>             p = raw_input(':')
>             print "Enter the telephone number type:(0 = UNKNOWN, 1 = HOME,
> 2 = WORK, 3 = FAX, 4 = CELL)"
>             t = raw_input(':')
>             if t == '0':
>                 foo.add(n, p, UNKNOWN)
>             if t == '1':
>                 foo.add(n, p, HOME)
>             if t == '2':
>                 foo.add(n, p, WORK)
>             if t == '3':
>                 foo.add(n, p, FAX)
>             if t == '4':
>                 foo.add(n, p, CELL)
>                 print t
>
>         elif choice == 2:
>             print(list)
>
>         loop = 0
> It works until I enter "t", then this is what I get:
> "Please make a selection from the following options:
>         To add an Employee - enter: 1
>
>         For a complete listing of Employees - enter: 2
>         To see the 'test page' and exit the database - enter: 3"
> :1
> Enter the Employee's full name:
> :john
> Enter the Employee's full telephone number:
> :123-456-7890
> Enter the telephone number type:(0 = UNKNOWN, 1 = HOME, 2 = WORK, 3 = FAX,
> 4 = CELL)
> :1
> First lookup:
> Traceback (most recent call last):
>   File "F:\CSC113 Module 4 CA\sample database.py", line 72, in <module>
>     class phonedb:
>   File "F:\CSC113 Module 4 CA\sample database.py", line 146, in phonedb
>     for entry in foo.lookup('reifsch'):
>   File "F:\CSC113 Module 4 CA\sample database.py", line 101, in lookup
>     if cmp(e, string) == 0:
> TypeError: comparison did not return an int
> >>>
> I do not know what this error means. I tried to look it up in help, but to
> no avail. If someone could please help or tell me where I could find an
> answer. Thanks.
>


Do you see the little part where it tells you? Your problem has nothing to
do with the stuff you showed us. It took place in
simple database.py, line 101. At the point "if cmp(e, string) == 0". First
of all, why are you doing cmp(e,string)==0? Use  "if e == string" instead.
It's the same thing (sometimes even calls the same code), but it's easier to
read. The problem was exactly what it said: the __cmp__ method in whatever
class e is needs to return an int. It isn't, so you get this problem Also,
in new code, it's considered better to use rich comparisons (__eq__, __ne__,
etc.) rather than __cmp__. That's another reason to use e == string. If e
uses __eq__, == will work. If e uses __cmp__, == will still work.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090228/2e55c0d1/attachment.html>


More information about the Python-list mailing list