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? - ##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()
-
-
3 38826 bvdet 2,851
Expert Mod 2GB
This line: - lname,fname,phone = getInput
should be: - lname,fname,phone = getInput()
The error message gives it away. The parentheses tell Python to call the preceding function name.
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.
bvdet 2,851
Expert Mod 2GB
You could do something like this: - >>> 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: - >>> if option == "2":
-
... print "2"
-
... elif option == "3":
-
... print "3"
-
...
-
2
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: Balaji |
last post by:
Hello Everybody...
I have a problem..
This is the code...
--------------------------------------
class Stack:
def __init__(self,expr):
self.stackP=
|
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...
|
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 ?...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |