Question: Project 7-3 Contact manager (first posting here: https://www.chegg.com/homework-help/questions-and-answers/project-7-3-contact-manager-python-uploaded-assignment-put-module-commands-bottom-informat-q108721053) Reworked my module with assistance from a friend that knows Python. Now I am stuck on why
Project 7-3 Contact manager (first posting here: https://www.chegg.com/homework-help/questions-and-answers/project-7-3-contact-manager-python-uploaded-assignment-put-module-commands-bottom-informat-q108721053)
Reworked my module with assistance from a friend that knows Python. Now I am stuck on why my "view" command hesitates to return the second entry until entered a second time.

Module
import csv FILENAME = "contacts.csv"
#define write to contacts.csv def write_contacts(contacts): with open(FILENAME, "w", newline="") as file: writer = csv.writer(file) writer.writerows(contacts) #def contacts def read_contacts(): contacts = [] with open(FILENAME, "r", newline="") as file: reader = csv.reader(file) for row in reader: contacts.append(row) return contacts
def view_contacts(contacts): index = int(input("Number: ")) if index len(contacts): print("Invalid contact number. Please try again. ") else: #print("Name:", row[0]) #print("Email:", row[1]) #print("Phone:", row[2]) print("Name: " + (contacts[index - 1][0])) print("Email: " + (contacts[index - 1][1])) print("Phone: " + (contacts[index - 1][2])) print()
#define list def list_contacts(contacts): #for i, contact in enumerate(contacts, start=1): for i in range(len(contacts)): contact = contacts[i] #print(f"{i}. {contact[0]} ({contact[1]})") print(str(i+1) + "." + contact [0]) print()
#add contacts 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. ")
#del contacts 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. ")
#Display command menu def display_menu(): print("Contact Manager") print() print("COMMAND MENU") print("list - Display call contacts") print("view - View a contact") print("add - Add a contact") print("del - Delete a contact") print("exit - Exit program ") print()
#define main def main(): display_menu() contacts = read_contacts() while True: command = input("Command: ") if command.lower() == "list": list_contacts(contacts) elif command.lower() == "view": number = input("Number: ") 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()
george curious was deleted. Command: list 1. Guido van Rossum 2. Eric Idle Command: view Number: 2 Number: 2 Name: Eric Idle Email: eric@ericidle.com Phone: +44207946 0958 Command
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
