inkedmn
01-27-2002, 01:31 AM
ok, here's my python address book. here is the problem.
after i've added an entry and it's saved to the list ok, i try to exit the program and write the new entry to the text file, it writes it, but there aren't any spaces in between the different 'fields'.
and when i try to reenter the program and pull up the saved entry, it does nothing.
i've read over this damn code a thousand times and i'm at my wit's end...
please help!!!
#!/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 " "
master = open('address.book', 'r')
master.readlines()
print "Address book loaded successfully."
print " "
return master
# 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]
#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()
quit == 1
elif x == 2:
y = raw_input("Please enter the last name: ")
if len(y) > 0:
findEntry(y)
else:
quit == 1
elif x == 3:
y = raw_input("Please enter the last name: ")
if len(y) > 0:
deleteEntry(y)
else:
quit == 1
elif x == 4:
y = raw_input("Please enter the last name: ")
if len(y) > 0:
editEntry(y)
quit == 1
elif x == 5:
closeApp()
quit == 0
sys.exit()
#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"
quit == 1
# this searches for an entry in the address book and returns matches
def findEntry(lastname):
for entry in master:
if entry[0] == lastname:
printEntry(entry)
else:
print "no matches"
#this searches for an entry to delete and prompts user for confirmation
def deleteEntry(lastname):
quit = 1
for entry in master:
if entry[0] == lastname:
printEntry(entry)
else:
print "no matches"
while quit == 1:
x = raw_input("Delete? y or n: ")
if x in yeslist:
entry = []
print "Deleted"
quit == 0
mainMenu()
elif x in nolist:
print "entry not deleted"
quit = 0
else:
print "invalid entry, try again"
quit == 1
# This allows an entry to be pulled from file and edited, then written (using addEntry function
# from earlier
def editEntry(lastname):
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()
# 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(list[1]),
newmaster.write(list[2]),
newmaster.write(list[3]),
newmaster.write(list[4]),
if len(list[5]) > 0:
newmaster.write(list[5])
newmaster.close
print "Entry saved successfully."
print "Goodbye..."
sys.exit()
######################################
## MAIN CODE
######################################
print "Welcome to PyAddressbook!"
print " "
startApp()
mainMenu()
thanks!
after i've added an entry and it's saved to the list ok, i try to exit the program and write the new entry to the text file, it writes it, but there aren't any spaces in between the different 'fields'.
and when i try to reenter the program and pull up the saved entry, it does nothing.
i've read over this damn code a thousand times and i'm at my wit's end...
please help!!!
#!/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 " "
master = open('address.book', 'r')
master.readlines()
print "Address book loaded successfully."
print " "
return master
# 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]
#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()
quit == 1
elif x == 2:
y = raw_input("Please enter the last name: ")
if len(y) > 0:
findEntry(y)
else:
quit == 1
elif x == 3:
y = raw_input("Please enter the last name: ")
if len(y) > 0:
deleteEntry(y)
else:
quit == 1
elif x == 4:
y = raw_input("Please enter the last name: ")
if len(y) > 0:
editEntry(y)
quit == 1
elif x == 5:
closeApp()
quit == 0
sys.exit()
#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"
quit == 1
# this searches for an entry in the address book and returns matches
def findEntry(lastname):
for entry in master:
if entry[0] == lastname:
printEntry(entry)
else:
print "no matches"
#this searches for an entry to delete and prompts user for confirmation
def deleteEntry(lastname):
quit = 1
for entry in master:
if entry[0] == lastname:
printEntry(entry)
else:
print "no matches"
while quit == 1:
x = raw_input("Delete? y or n: ")
if x in yeslist:
entry = []
print "Deleted"
quit == 0
mainMenu()
elif x in nolist:
print "entry not deleted"
quit = 0
else:
print "invalid entry, try again"
quit == 1
# This allows an entry to be pulled from file and edited, then written (using addEntry function
# from earlier
def editEntry(lastname):
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()
# 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(list[1]),
newmaster.write(list[2]),
newmaster.write(list[3]),
newmaster.write(list[4]),
if len(list[5]) > 0:
newmaster.write(list[5])
newmaster.close
print "Entry saved successfully."
print "Goodbye..."
sys.exit()
######################################
## MAIN CODE
######################################
print "Welcome to PyAddressbook!"
print " "
startApp()
mainMenu()
thanks!