I can not get this program to list all of the contact names, it just list the first one.

##!/usr/bin/evn python3
#
# Contact Manager Program
#
import csv

FILENAME = "contacts.csv"

def write_contacts(contacts):
    with open (FILENAME, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerows(contacts)

def read_contacts():
    contacts = []
    with open(FILENAME, newline="") as file:
        reader = csv.reader(file)
        for row in reader:
            contacts.append(row)
            return contacts

def list_contacts(contacts):
    for i in range(len(contacts)):
        contact = contacts[i]
        print(str(i+1) + "." + contact[0] )
        print()

def view_contacts(contacts):
    number = int(input("Number: "))
    if number < 1 or number > len (contacts):
        print("invalid number")

    else:

            contact = contacts[number-1]
            print("Name: " + contact[0])
            print("Email: " + contact[1])
            print("Phone: " + contact[2])
            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(name + " was added")
    print()

def delete_contacts(contacts):
    number = int(input("Number: "))
    if number < 0 or number > len (contacts):
        print("invalid number")
    else:
            contact = contacts.pop(number-1)
            write_contacts(contacts)
            print(contact[0] + " was deleted")
            return contacts

def display_menu():
    print("Contact Manager")
    print()
    print("COMMAND MENU")
    print("list - Display all contacts", "\nview - View a contact",
          "\nadd - Add a contact", "\ndel - Delete a contact",
          "\nexit - 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":
            print("Good bye!")
            break
        else:
            print("Invalid command. Please try again.\n")

if __name__ == "__main__":
    main()

Recommended Answers

All 3 Replies

Print "contacts" to see what it contains

In the menu routine, add a line "There are x contacts." so you can see if you have one or more in that array.

Hi, what was your final solution?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.