473,749 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TypeError: 'function' object is not iterable

bferguson94
11 New Member
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 38909
bvdet
2,851 Recognized Expert Moderator Specialist
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
bferguson94
11 New Member
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 Recognized Expert Moderator Specialist
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
3164
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 object out of the code object. The function object can then be turned into a method for example using types.MethodType(). Right? Well, on Windows 98, using python 2.2.2 (or 2.3b2): >>> c=compile('def a(msg): return msg','<nowhere>','exec') >>>...
1
13103
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 statements calling the class objects in my extention DLL. 3. Before each execution I import my extention module using;
2
3544
by: Balaji | last post by:
Hello Everybody... I have a problem.. This is the code... -------------------------------------- class Stack: def __init__(self,expr): self.stackP=
5
22736
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 StateMachineName is of type str in the Namespace |
1
1706
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 ? urlparse('http://www.cwi.nl:80/%7Eguido/Python.html') TypeError: 'module' object is not callable ****************************************************************
9
80774
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 "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript exec codeObject in __main__.__dict__ File "Q:\PythonScripts\InventoryCompareCase.py", line 276, in ? main()
2
6731
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 def index(self, pageid = None): selection = html.Page() return selection.input()
3
1946
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 with a throw. Er, raise.
33
56487
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 have a Step.py class: class Step(object) def __init__(self, sName): "Initialise a new Step instance"
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.