在我陳述我的問題之前,我想先說我是 Python 編程的初學(xué)者。我的第一堂編程課已經(jīng)過了一半。話雖如此,我還研究并使用了搜索引擎來查找有關(guān)我正在研究的主題的信息,但我沒有找到任何對我的問題有幫助或足夠具體的信息。我瀏覽了 Stack Overflow,包括瀏覽類似的問題對話。我希望在我得到任何有用的信息之前,這不會被否決或標(biāo)記為重復(fù)。我正在創(chuàng)建一個聯(lián)系人管理器程序,該程序使用存儲在 CSV 文件中的聯(lián)系人姓名、電子郵件地址和電話號碼列表。我的程序應(yīng)該允許用戶顯示所有聯(lián)系人姓名的列表、添加/刪除聯(lián)系人以及查看特定的聯(lián)系人信息。我對最終要求有問題。程序中的其他所有內(nèi)容都可以正常工作并在控制臺中正常顯示。整個程序的代碼如下;#!/user/bin/env python3# Contacts Manager Program#Shows title of program at start.print("The Contact Manager Program")print()#Imports CSV Moduleimport 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")我需要 view_contact 函數(shù)從用戶那里獲取一個數(shù)字作為輸入,然后打印與 CSV 文件中的行號相關(guān)的相應(yīng)聯(lián)系信息。
1 回答

心有法竹
TA貢獻(xiàn)1866條經(jīng)驗 獲得超5個贊
看起來您在 .csv 文件中以列表的形式存儲聯(lián)系人。用于read_contacts從該 csv 文件中讀取所有聯(lián)系人,然后獲取由 number 參數(shù)指定的聯(lián)系人。而已。
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])
添加回答
舉報
0/150
提交
取消