任务是创建一个程序,该程序允许用户输入朋友的姓名和电话号码,然后打印出按姓氏排序的联系人列表。还可以使用功能。
我的问题是它仅要求用户执行一项操作,然后立即要求详细信息。它应该要求用户选择其他动作。退出,添加联系人,显示联系人或对联系人进行排序。
def menu():
'''Display Menu'''
print(
"""
Contact Lists
0 - Exit
1 - Show Contacts
2 - Add Contacts
3 - Sort Contacts
"""
)
def ask():
user = None
user = input("Action: ")
print()
return user
def main():
menu()
action = ask()
names = []
while action != 0:
if action == "0":
print("Closing Contact Lists.")
elif action == "1":
print("Contact Lists: ")
for name in names:
print(name)
#setting a condition if user enter "2" it will let user add name, last name and phone number
elif action == "2":
name = input("Add contact's first name: ") #input 1
last_name = input("Add contact's last name: ") #input 2
contact_number = input("Add phone number for the contact name: ") #input 3
entry = (last_name, name, contact_number)
names.append(entry)
#setting a condition if user enter "3" it will sort contact list according to last names
elif action == "3":
entry.sort(reverse=True) #use of sort() to sort lists of by last names
print(names)
else:
print("Invalid Action!")
main()
答案 0 :(得分:0)
您的代码中有两个错误:
尝试将action = ask()
添加到您最后的else
条件下。
编辑
代码段:
#setting a condition if user enter "3" it will sort contact list according to last names
elif action == "3":
entry.sort(reverse=True) #use of sort() to sort lists of by last names
print(names)
else:
print("Invalid Action!")
action = ask()
names
(一个列表)上执行,而不是在entry
(一个元组)上执行