Click to See Complete Forum and Search --> : please tell me what i'm doing wrong...


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!

inkedmn
01-27-2002, 03:35 AM
update:


#!/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 = []
master = open('address.book', 'r')
for line in master.readlines():
result.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]

#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(" ")
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])
newmaster.close
sleep(3)
print "Goodbye..."
sys.exit()

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

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

Gnu/Vince
01-27-2002, 11:39 AM
If you save something and enter the program again and exit again, the file is empty.

kmj
01-27-2002, 01:36 PM
Well, for starters... :)

(this doesn't have to do with your above mentioned problem:
Your addEntry function has some major issues... it never exits! You use a variable quit to control the loop, but then you never really use the variable. It never gets changed; you try to set it to 1 in the two branches of the code where you want to continue looping, but you don't: you use 'quit == 1' instead of 'quit = 1'; 'quit == 1' doesn't do anything, and it probably isn't even valid code. Then, instead of setting quit to 0 to leave the function, you just call the main menu function again. I don't think that's good coding style. The main menu called addEntry to add an entry; when it's done, it should just leave the function and go back into the main menu (not call the main menu function again.)

Also, I noticed you have similar issues with the main menu function, you have the line:

quit == 1

through. That line doesn't do anything, and should be fixed, everywhere in your code.

Also, you shouldn't need sys.exit() at all. When the 'exit' option has specified, you should just call the closeApp function, and set quit to zero. When closeApp is done, it should just return (not call sys.exit()).. Then when the main menu exits, the the function will return, and since that's the last line of code in the program, the program will exit.

This probably won't fix your problem, but it'll make your code a little clearer...

Gnu/Vince
01-27-2002, 01:57 PM
I suggest to use infinite loops (while 1: ) and to use the word "break" to leave the loop when you need to.

[ 27 January 2002: Message edited by: Gnu/Vince ]

kmj
01-27-2002, 03:36 PM
Originally posted by Gnu/Vince:
<STRONG>I suggest to use infinite loops (while 1: ) and to use the word "break" to leave the loop when you need to.

[ 27 January 2002: Message edited by: Gnu/Vince ]</STRONG>

It's just as easy to use 'while quit == 1:' and 'quit = 0' to leave the loop; and academic types generally consider that to be cleaner.

kmj
01-27-2002, 11:43 PM
Oh, btw..

the write(...) method of the File object, doesn't write out a newline like the print command does, so if each entry is a seperate line, you'll want to write out a newline after you write out each entry.

and the reason (I think) that it may not be working is that you're not actually calling the 'close' function. If you give a function name without the parenthesis (which you're doing, if you check your code), you are referencing that function object, not actually calling the function.

:) try that and see if maybe your code will work better now.

Energon
01-28-2002, 06:39 PM
Originally posted by kmj:
<STRONG>It's just as easy to use 'while quit == 1:' and 'quit = 0' to leave the loop; and academic types generally consider that to be cleaner.</STRONG>

There any reason for that? Or is it just because they like a good irrelevant argument?

kmj
01-28-2002, 08:09 PM
Originally posted by Energon:
<STRONG>There any reason for that? Or is it just because they like a good irrelevant argument?</STRONG>

because a break is similar to a goto, so they say it's bad. (I know function calls, function returns, and loops are also similar to goto, but they're higher level)