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...
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()
Comment