【发布时间】:2019-03-26 19:30:19
【问题描述】:
在我陈述我的问题之前,我想先说我是 Python 编程的初学者。我的第一堂编程课已经完成了一半。话虽如此,我还研究并使用搜索引擎来查找有关我正在研究的主题的信息,但我没有找到任何对我的问题有帮助或足够具体的信息。我浏览了 Stack Overflow,包括浏览类似的问题对话。我希望在我得到任何有用的信息之前,这不会被否决或标记为重复。
我正在创建一个联系人管理器程序,该程序使用存储在 CSV 文件中的联系人姓名、电子邮件地址和电话号码列表。我的程序应该允许用户显示所有联系人姓名的列表、添加/删除联系人以及查看特定的联系人信息。我在最终要求方面遇到了麻烦。程序中的其他所有内容都在控制台中正常工作和显示。整个程序的代码如下;
#!/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 文件中的行号相关的相应联系信息。
【问题讨论】:
-
问题是什么,您尝试过什么?
-
欢迎来到 Stack Overflow!请使用tour 并阅读How to Ask。关于您的问题,请更具体并删除不相关的部分。通常,这也意味着从您的代码中提取minimal reproducible example。
标签: python list csv user-input display