Question
Question here: https://www.chegg.com/homework-help/questions-and-answers/received-help-stating-put-following-information-define-list-area-keying-get-error-screensh-q108800234 ---- has already been figured out(cannot edit the post for some reason). New question on 8-3: Have fixed errors from above ^^
Question here: https://www.chegg.com/homework-help/questions-and-answers/received-help-stating-put-following-information-define-list-area-keying-get-error-screensh-q108800234 ---- has already been figured out(cannot edit the post for some reason).
New question on 8-3: Have fixed errors from above ^^ and added missing "There are no contacts in the list". Since adding "no contacts", I cannot get the module to kick back the list when requesting the list. Module listed and screenshot of output:
import csv import sys FILENAME = "contacts.csv"
def write_contacts(contacts): try: with open(FILENAME, "w", newline="") as file: writer = csv.writer(file) writer.writerows(contacts) except Exception as e: print(type(e), e) exit_program()
def read_contacts(): try: contacts = [] with open(FILENAME, "r", newline="") as file: reader = csv.reader(file) for row in reader: contacts.append(row) return contacts except FileNotFoundError as e: print("There are no " + FILENAME + " in the list.") exit_program()
def view_contacts(contacts): while True: try: index = int(input("Number: ")) except ValueError: print("Invalid integer.") continue if index len(contacts): print("Invalid contact number. Please try again. ") else: break print("Name: " + (contacts[index -1][0])) print("Email: " + (contacts[index -1][1])) print("Phone: " + (contacts[index - 1][2])) print()
def list_contacts(contacts): if (len(contacts) > 0): for i in range(len(contacts)): contact = contacts[i] if (len(contact) > 0): print(str(i+1) + "." + contact [0]) else: print("There are no contacts in the list.") print() return #else: #print("There are no contacts in the list.") #print()
def add_contacts(contacts): name = input("Name: ") email = input("Email: ") phone = input("Phone: ") contact = [] contact.append(name) contact.append(email) contact.append(phone) contacts.append(contact) write_contacts(contacts) print(f"{name} was added. ") def delete_contacts(contacts): number = int(input("Number: ")) if number len(contacts): print("Invalid contact number. Please try again. ") else: contact = contacts.pop(number -1) write_contacts(contacts) print(f"{contact[0]} was deleted. ")
def exit_program(): print("Terminating program.") sys.exit()
def display_menu(): print("Contact Manager") print() print("Could not find contacts file!") print("Starting a new contacts file ..") print() print("Command Menu") print("list - Display all contacts") print("view - Display a contact") print("add - Add a contact") print("del - Delete a contact") print("exit - Exit program ") print()
def main(): display_menu() contacts = read_contacts() while True: command = input("Command: ") if command.lower() == "list": list_contacts(contacts) elif command.lower() == "view": view_contacts(contacts) elif command.lower() == "add": add_contacts(contacts) elif command.lower == "del": delete_contacts(contacts) elif command.lower == "exit": break else: print("Not a valid command. Please try again. ") print("Bye!")
if __name__ == "__main__": main()
Why does my script not return the list after I have added a contact? instead, it says no contacts in list again.
Command: list There are no contacts in the list. Command: add Name: aslj Email: skdj we Phone: sletj w aslj was added. Command: list There are no contacts in the list. CommandStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started