473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

TypeError: 'function' object is not iterable

bferguson94
This program is supposed to mimic a phone book. This program also needs to be menu driven. The general idea is to accept a first name, last name, or phone number from the user and to read through a TEXT file,looking for a match. If you look at my program and what I have so far, it is easy to see that the text file location is ...C:\python26\entries.txt and can be easily madeup. If you were to open the text file it would reas as follows: jones\n tom\n 208-203-3450\n johnson\n rob\n 345-324-1234\n and so on...
I think this program should work, but for some reason i keep getting a "TypeError: 'function' object is not iterable which is keeping my program from accesing the text document..

any suggestions?

Expand|Select|Wrap|Line Numbers
  1.  ##call to input file and return
  2. ##also calls to "lookup" modules for output
  3.  
  4. def main():
  5.     myLastName = []
  6.     myFirtName = []
  7.     myPhoneNumber = []
  8.     lname,fname,phone = getInput
  9.  
  10.     choice = 0
  11.  
  12. ##menu options
  13.     while choice !='4':
  14.         choice = raw_input('1.lookup by last name\n 2.lookup by first name\n 3.lookup by phone number\n 4.quit')
  15.         if choice == '1':
  16.             getLname(lname, fname, phone)
  17.         if choice == '2':
  18.             getFname (lname, fname, phone)
  19.         if choice == '3':
  20.             getPhone (lname, fname, phone)
  21.         if int(choice) < 0 :
  22.             print "invalid option"
  23.         if int (choice) > 4:
  24.             print "invalid option"
  25.  
  26.  
  27. def getInput():
  28.     myfile= "c:\\python26\\entries.txt"
  29.     fileInput = open(myFile, "r")
  30.     count = 0
  31.     for mystring in fileInput:
  32.         myString = myString.strip()
  33.         myString = myString.lower()
  34.         myNum = count % 3
  35.         if myNum == 0:
  36.             myLastName.append(myString)
  37.         elif myNum == 1:
  38.             myFirstname.append(myString)
  39.         elif myNum == 2:
  40.             myPhoneNumber.append(myString)
  41.         count = count + 1
  42.         return
  43.     fileInput.close
  44.  
  45.  
  46.  
  47. def getLname(lname, fname, phone):
  48.     name = raw_input("last name to lookup:").strip().lower()
  49.  
  50.     pointer = 0
  51.     if name in lname:
  52.         while True:
  53.             try:
  54.                 pointer = lname.index(name, pointer)
  55.                 print fname[pointer].title(),
  56.                 print lame[pointer].title()
  57.                 print phone[pointer]
  58.             except:
  59.                 break
  60.     else:
  61.         print "no entry found for" + name.title()
  62.  
  63. def getFname(lname, fname, phone):
  64.     name = raw_input("first name to lookup:").strip().lower()
  65.  
  66.     pointer = 0
  67.     if name in fname:
  68.         while True:
  69.             try:
  70.                 pointer = fname.index(name, pointer)
  71.                 print fname[pointer].title(),
  72.                 print lame[pointer].title()
  73.                 print phone[pointer]
  74.             except:
  75.                 break
  76.     else:
  77.         print "no entry found for" + name.title()
  78.  
  79. def getPhone(lname, fname, phone):
  80.     name = raw_input("phone number to lookup:").strip().lower()
  81.  
  82.     pointer = 0
  83.     if name in phone:
  84.         while True:
  85.             try:
  86.                 pointer = phone.index(name, pointer)
  87.                 print fname[pointer].title(),
  88.                 print lame[pointer].title()
  89.                 print phone[pointer]
  90.             except:
  91.                 break
  92.     else:
  93.         print "no entry found for" + name.title()
  94.  
  95. main()
  96.  
  97.  
Mar 12 '10 #1
3 38826
bvdet
2,851 Expert Mod 2GB
This line:
Expand|Select|Wrap|Line Numbers
  1.     lname,fname,phone = getInput
should be:
Expand|Select|Wrap|Line Numbers
  1.     lname,fname,phone = getInput()
The error message gives it away. The parentheses tell Python to call the preceding function name.
Mar 13 '10 #2
ya that was it..

thanks!!

do you know how i could catch invalid user input on the menu that I have?? I have it programmed to catch any numbers out of range " < 0 and >4.. but it doesn't catch chars.
Mar 15 '10 #3
bvdet
2,851 Expert Mod 2GB
You could do something like this:
Expand|Select|Wrap|Line Numbers
  1. >>> while True:
  2. ...     option = raw_input("Enter 1,2,3 or 4")
  3. ...     print option
  4. ...     if option in ['1', '2', '3', '4']:
  5. ...         break
  6. ...     else:
  7. ...         print "Invalid option"
  8. ...         
  9. 6
  10. Invalid option
  11. A
  12. Invalid option
  13. 2
  14. >>> 
Use an if/elif block instead of several if blocks. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> if option == "2":
  2. ...     print "2"
  3. ... elif option == "3":
  4. ...     print "3"
  5. ...     
  6. 2
  7. >>> 
Mar 15 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Petri Savolainen | last post by:
After reading the manuals and googling around a bit, I thought I'd use the 'compile' built-in to create a code object. Then, using either new.function() or types.FunctionType(), create a function...
1
by: Atul Kshirsagar | last post by:
Hello, I am using Python 2.3.2 with a C++ extention DLL in muti-threaded environment. 1. For each new thread I create a separate sub-interpreter. 2. Each thread executes multiple python...
2
by: Balaji | last post by:
Hello Everybody... I have a problem.. This is the code... -------------------------------------- class Stack: def __init__(self,expr): self.stackP=
5
by: Randall Parker | last post by:
Using Python 2.4.2 on Windows 2000 in SPE. Getting: TypeError: 'str' object is not callable on this line: TmpErrMsg1 = "State machine %s " (StateMachineName) In Winpdb 1.0.6 the...
1
by: Gary Wessle | last post by:
dear python users I am not sure why I am getting **************************************************************** Traceback (most recent call last): File "my.py", line 3, in ?...
9
by: k.retheesh | last post by:
Can anybody tell me why am I getting this error message while trying to print a part of a string. Is there a better approach for this... Traceback (most recent call last): File...
2
by: AWasilenko | last post by:
I'm trying to test a few different approaches to displaying pages via Cherrypy and I'm not having much luck. Here is my code so far: import sys, cherrypy, html class Root: @cherrypy.expose...
3
by: Aaron Brady | last post by:
>>f.func_defaults Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment V. interesting. Operation succeeds but...
33
by: christophertidy | last post by:
Hi I am new to Python and have recieved this error message when trying to instantiate an object from a class from another file within the same directory and wondered what I have done wrong. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.