[Tutor] index list out of range
pythonhack@yahoo.com
pythonhack@yahoo.com
Thu, 31 Jan 2002 16:52:01 -0800
hey guys-
i'm writing a small address book program, and i think there's a
problem with the way the lists are structured and manipulated. i'm
getting a "list index out of range" error when i try to exit the
program (using the closeApp function).
help!
here's the code...
#!/usr/bin/env python
import sys, string, os
from time import sleep
newmaster = []
master = []
yeslist = ['y', 'Y', 'YES', 'yes']
nolist = ['n', 'N', 'NO', 'no']
##################################
## FUNCTIONS
##################################
#this function runs at the beginning of the script
#reads entire master address book into memory
def startApp():
print "Loading address book..."
sleep(3)
print " "
result = open('address.book', 'r')
for line in result.readlines():
master.append(line.split())
print "Address book loaded successfully."
print " "
return result
# print function returns formatted output
def printEntry(entry):
print entry[0], entry[1]
print entry[2]
print entry[3]
print entry[4]
if len(entry[5]) > 0:
print entry[5]
else:
print "<no comment>"
mainMenu()
#the main menu
def mainMenu():
quit = 1
while quit == 1:
print "What would you like to do? - Please select "
print "1. Add an entry"
print "2. Display an entry"
print "3. Delete an entry"
print "4. Edit an entry"
print "5. Exit the program"
x = int(raw_input())
if x == 1:
addEntry()
elif x == 2:
findEntry()
elif x == 3:
deleteEntry()
elif x == 4:
editEntry()
elif x == 5:
closeApp()
else:
print "Invalid Selection."
print " "
quit == 1
#adds an entry to the address book
def addEntry():
newentry = []
print " "
print "You have chosen to add an entry to the address book."
print " "
lastname = raw_input("Last Name: ")
firstname = raw_input("First Name: ")
address1 = raw_input("Street Name and Number: ")
address2 = raw_input("City, State, Zip: ")
phone = raw_input("Phone Number: ")
comments = raw_input("Comments (if none, juts hit enter): ")
print " "
print "This is what you entered: "
print " "
print lastname,',', firstname
print address1
print address2
print phone
if len(comments) >= 1:
print comments
else:
print "<no comment>"
print " "
correct = raw_input("is this correct?(y/n): ")
if correct == 'y':
print "adding new entry..."
newentry.append(lastname)
newentry.append(firstname)
newentry.append(address1)
newentry.append(address2)
newentry.append(phone)
newentry.append(comments)
master.append(newentry)
newentry = []
print "new entry added successfully"
mainMenu()
elif correct == 'n':
print "Please re-enter information..."
addEntry()
else:
print "Invalid response."
print " "
mainMenu()
# this searches for an entry in the address book and returns matches
def findEntry():
quit = 1
while quit == 1:
print "Enter Last Name (or just hit enter to return to Main Menu): "
name = raw_input()
if len(name) > 0:
for entry in master:
if entry[0] == name:
printEntry(entry)
else:
print "no matches"
else:
mainMenu()
#this searches for an entry to delete and prompts user for confirmation
def deleteEntry():
print "Enter Last Name (or just hit enter to return to Main Menu): "
name = raw_input()
quit = 1
if len(name) > 0:
for entry in master:
if entry[0] == name:
printEntry(entry)
while quit == 1:
x = raw_input("Delete? y or n: ")
if x in yeslist:
entry = []
print "Deleted"
mainMenu()
elif x in nolist:
print "entry not deleted"
mainMenu()
else:
print "invalid entry, try again"
quit == 1
else:
print "no matches"
mainMenu()
else:
mainMenu()
# This allows an entry to be pulled from file and edited, then written (using addEntry function
# from earlier
def editEntry():
for entry in master:
if entry[0] == lastname:
printEntry(entry)
x = raw_input("Edit this entry? y or n: ")
if x in yeslist:
entry = []
addEntry()
else:
mainMenu()
# This is run when the program exits, writes all entries back to text file and closes text file
def closeApp():
print "Saving address book changes..."
newmaster = open('address.book', 'w')
for list in master:
print len(list)
newmaster.write(list[0]),
newmaster.write(" ")
newmaster.write(list[1]),
newmaster.write(" ")
newmaster.write(list[2]),
newmaster.write(" ")
newmaster.write(list[3]),
newmaster.write(" ")
newmaster.write(list[4]),
newmaster.write(" ")
print "closing address.book..."
newmaster.close
sleep(3)
print "Goodbye..."
sys.exit()
######################################
## MAIN CODE
######################################
print "Welcome to PyAddressbook!"
print " "
startApp()
mainMenu()
thanks!!
brett
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com