Click to See Complete Forum and Search --> : almost got it...


inkedmn
01-31-2002, 07:30 PM
ok, me again :)

the address book program. i've got it to the point where i can save entries and close the program and pull them up again, but it's not exiting properly (i think i resolved the other issues with the quit variable, etc.). but it's telling me "list index out of range"

what does this mean?

here's the code. AGAIN.


#!/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:
pass
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 = []
quit = 1
while quit == 1:
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
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..."
quit == 1
else:
print "Invalid response, please try again"
print " "
quit == 1

# 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:
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]),
if len(list[5]) > 0:
newmaster.write(" ")
newmaster.write(list[5])
else:
pass
newmaster.close
sleep(3)
print "Goodbye..."
sys.exit()

######################################
## MAIN CODE
######################################

print "Welcome to PyAddressbook!"
print " "
startApp()
mainMenu()


help, please
:( :( :(

kmj
01-31-2002, 08:17 PM
first thing:

what are you trying to do with the quite variable in your mainMenu while loop? Because it isn't doing anything. it's the same as saying "while 1:"...


second... python should be telling you what line of code (showing you the exact line) where you're getting a 'list index out of range' error. That should be a major hint; if you still need help, then find out what that line is and tell us; that'll give us alot more to go on.

also -> are you sure, in your closeApp function, that every list in master has at least six elements? you might want to add the line "print len(list)" for debugging purposes just to verify that they've all got at least that many items.