使用用户输入显示列表中的特定信息 - Python 3

Displaying Specific Information from a List in using User Input - Python 3

在陈述我的问题之前,我想首先说明我是 Python 编程的初学者。我的第一次编程已经进行了一半 class。话虽如此,我也研究并使用了搜索引擎来查找有关我正在研究的主题的信息,但我没有发现任何对我的问题有帮助或足够具体的信息。我浏览了 Stack Overflow,包括浏览类似问题的对话。我希望在我获得任何有用的信息之前,这不会被否决或标记为重复。

我正在创建一个联系人管理器程序,该程序使用存储在 CSV 文件中的联系人姓名、电子邮件地址和 phone 号码列表。我的程序应该允许用户显示所有联系人姓名、add/delete 联系人的列表,并查看特定的联系人信息。我在满足最终要求时遇到了麻烦。程序中的所有其他内容都正常工作并按应有的方式显示在控制台中。整个程序的代码如下;

#!/user/bin/env python3

# Contacts Manager Program

#Shows title of program at start.
print("The Contact Manager Program")
print()

#Imports CSV Module
import csv

#Defines global constant for the file.
FILENAME = "contacts.csv"

#Displays menu options for user, called from main function.
def display_menu():
    print("COMMAND MENU")
    print("list - Display all contacts")
    print("view - View a contact")
    print("add - Add a contact")
    print("del - Delete a contact")
    print("exit - Exit program")
    print()

#Defines write funtion for CSV file.
def write_contacts(contacts):
    with open(FILENAME, "w", newline="") as file:
        writer = csv.writer(file)
        writer.writerows(contacts)

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

#Lists the contacts in the list with the user inputs the "list" command.    
def list_contacts(contacts):
    for i in range(len(contacts)):
        contact = contacts[i]
        print(str(i+1) + ". " + contact[0])
    print()

#List a specific contacts information when the user inputs the "view" command.
def view_contact(number):

#Adds contact to the end of the list when user inputs the "add" command.
def add_contact(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.\n")

#Removes an item from the list.
def delete_contact(contacts):
    number = int(input("Number: "))
    if number < 1 or number > len(contacts):                         #Display an error message if the user enters an invalid number.
        print("Invalid contact number.\n")
    else:
        contact = contacts.pop(number-1)
        write_contacts(contacts)
        print(contact[0] + " was deleted.\n")

#Main function - list, view, add, and delete funtions run from here. 
def main():
    display_menu()
    contacts = read_contacts()
    while True:
        command = input("Command: ")
        if command.lower() == "list":
            list_contacts(contacts)
        elif command.lower() == "view":                                #Store the rest of the code that gets input and displays output in the main function. 
            view_contact(contacts)
        elif command.lower() =="add":
            add_contact(contacts)
        elif command.lower() == "del":
            delete_contact(contacts)
        elif command.lower() == "exit":                                
            break
        else:
            print("Not a valid command. Please try again.\n")

    print("Bye!")

if __name__ == "__main__":
    main()

我需要 view_contact 函数来获取用户输入的数字,然后打印与 CSV 文件中的行号相关的相应联系信息。

您似乎在 .csv 文件中以列表形式存储联系人。使用 read_contacts 从该 csv 文件中读取所有联系人,然后获取由数字参数指定的联系人。就是这样。

def view_contact(number):
    contacts = read_contacts()
    specified_contact = contacts[number]
    print("Name: ", specified_contact[0])
    print("Email: ", specified_contact[1])
    print("Phone: ", specified_contact[2])