TypeError: 'function' object is not iterable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bferguson94
    New Member
    • Feb 2010
    • 11

    TypeError: 'function' object is not iterable

    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?

    Code:
     ##call to input file and return
    ##also calls to "lookup" modules for output
    
    def main():
        myLastName = []
        myFirtName = []
        myPhoneNumber = []
        lname,fname,phone = getInput
    
        choice = 0
    
    ##menu options
        while choice !='4':
            choice = raw_input('1.lookup by last name\n 2.lookup by first name\n 3.lookup by phone number\n 4.quit')
            if choice == '1':
                getLname(lname, fname, phone)
            if choice == '2':
                getFname (lname, fname, phone)
            if choice == '3':
                getPhone (lname, fname, phone)
            if int(choice) < 0 :
                print "invalid option"
            if int (choice) > 4:
                print "invalid option"
    
    
    def getInput():
        myfile= "c:\\python26\\entries.txt"
        fileInput = open(myFile, "r")
        count = 0
        for mystring in fileInput:
            myString = myString.strip()
            myString = myString.lower()
            myNum = count % 3
            if myNum == 0:
                myLastName.append(myString)
            elif myNum == 1:
                myFirstname.append(myString)
            elif myNum == 2:
                myPhoneNumber.append(myString)
            count = count + 1
            return
        fileInput.close
    
    
    
    def getLname(lname, fname, phone):
        name = raw_input("last name to lookup:").strip().lower()
    
        pointer = 0
        if name in lname:
            while True:
                try:
                    pointer = lname.index(name, pointer)
                    print fname[pointer].title(),
                    print lame[pointer].title()
                    print phone[pointer]
                except:
                    break
        else:
            print "no entry found for" + name.title()
    
    def getFname(lname, fname, phone):
        name = raw_input("first name to lookup:").strip().lower()
    
        pointer = 0
        if name in fname:
            while True:
                try:
                    pointer = fname.index(name, pointer)
                    print fname[pointer].title(),
                    print lame[pointer].title()
                    print phone[pointer]
                except:
                    break
        else:
            print "no entry found for" + name.title()
                               
    def getPhone(lname, fname, phone):
        name = raw_input("phone number to lookup:").strip().lower()
    
        pointer = 0
        if name in phone:
            while True:
                try:
                    pointer = phone.index(name, pointer)
                    print fname[pointer].title(),
                    print lame[pointer].title()
                    print phone[pointer]
                except:
                    break
        else:
            print "no entry found for" + name.title()
                               
    main()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    This line:
    Code:
        lname,fname,phone = getInput
    should be:
    Code:
        lname,fname,phone = getInput()
    The error message gives it away. The parentheses tell Python to call the preceding function name.

    Comment

    • bferguson94
      New Member
      • Feb 2010
      • 11

      #3
      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.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        You could do something like this:
        Code:
        >>> while True:
        ... 	option = raw_input("Enter 1,2,3 or 4")
        ... 	print option
        ... 	if option in ['1', '2', '3', '4']:
        ... 		break
        ... 	else:
        ... 		print "Invalid option"
        ... 		
        6
        Invalid option
        A
        Invalid option
        2
        >>>
        Use an if/elif block instead of several if blocks. Example:
        Code:
        >>> if option == "2":
        ... 	print "2"
        ... elif option == "3":
        ... 	print "3"
        ... 	
        2
        >>>

        Comment

        Working...