I have a program to make an address book, and I want to be able to confirm a change to a record before doing so--if I search for the last name "Peterson" and there are two entries, I can choose to change one, both, or neither. I'm trying to use basically the same code to either edit an existing line, or delete it from the program. I'm new to Python and this is my final project for a class, and I've spent like four days trying to figure out what isn't working. I've been looking through Stack Overflow and haven't found a satisfactory answer to my problem, and I am sure that's because I don't understand Python well enough. We're supposed to use the set up of creating and renaming a temp file, so while I know that's not the most efficient, it's what I'm supposed to do.
This is what I have:
import os
FIRSTNAME = 0
LASTNAME = 1
STREETADDRESS = 2
CITY = 3
STATE = 4
ZIP = 5
TELEPHONE1 = 6
TELEPHONE2 = 7
def modify():
found = False
search = input("Enter an item to search for: ")
new = input("And what should we change it to? ")
addressbook = open("addressbook.txt", "r")
temp_file = open("temp.txt", "w")
line = addressbook.readline()
while line != "":
line = line.rstrip("\n")
lineInfo = line.split("|")
if lineInfo[1] == search:
print("I found it!")
print(format(lineInfo[FIRSTNAME], '15s'),format(lineInfo[LASTNAME], '15s'),format(lineInfo[STREETADDRESS], '20s'),format(lineInfo[CITY], '10s'),
format(lineInfo[STATE], '5s'),format(lineInfo[ZIP], '10s'),format(lineInfo[TELEPHONE1], '15s')," ",format(lineInfo[TELEPHONE2], '10s'))
print()
delete = input("change this one? press y for yes.")
if delete == "y":
found = True
lineInfo[1] = new
temp_file.write(format(lineInfo[FIRSTNAME])+"|")
temp_file.write(format(lineInfo[LASTNAME])+"|")
temp_file.write(format(lineInfo[STREETADDRESS])+"|")
temp_file.write(format(lineInfo[CITY])+"|")
temp_file.write(format(lineInfo[STATE])+"|")
temp_file.write(format(lineInfo[ZIP])+"|")
temp_file.write(format(lineInfo[TELEPHONE1])+"|")
temp_file.write(format(lineInfo[TELEPHONE2])+"|")
temp_file.write("\n")
else:
temp_file.write(line)
temp_file.write("\n")
else:
temp_file.write(line)
temp_file.write("\n")
line = addressbook.readline()
temp_file.close()
os.rename("temp.txt","newaddress.txt")
if found:
print("File has been changed")
else:
print("File was not found")
modify()
Presently when I run it, I get this:
Enter an item to search for: Peterson
And what should we change it to? Patterson
I found it!
Edward Peterson 10 Grand Pl
Kearny NJ 90031 383-313-3003 xxx
change this one? press y for yes.n
I found it!
James Peterson 11 Grand Pl
Kearny NJ 90021 xxx xxx
change this one? press y for yes.y
Traceback (most recent call last):
File "C:\Users\kendr\Desktop\Address Book\Delete Address Book.py", line 53, in <module>
delete()
File "C:\Users\kendr\Desktop\Address Book\Delete Address Book.py", line 22, in delete
if lineInfo[1] == search:
IndexError: list index out of range
Honestly I'm at my wit's end with this assignment, so any and all help would make a huge difference. Thanks, K