我正在尝试创建一个电话簿,除了我需要一遍又一遍地运行整个代码而不是单独的 if 语句之外,我拥有一切。我不知道如何继续任何建议?
我使用了 while 循环。
#Intro
print("Address book to store friends contact")
print("-" * 50)
print("-" * 50)
#Display of options
print("Select an option: ")
print("1-Add/Update contact")
print("2- Display all contacts")
print("3- Search")
print("4- Delete contact")
print("5- Quit")
#Selection
option = input("Select which one you would like to choose. Ex. Select an
option. Type here: ")
#Map
contacts = {}
#Main program
for i in range(0,1):
if option == "Add/Update contact":
person_added = input("Who would you like to be updated or added")
next_question = input("What is there contact information")
#The code below will add the person to the list or update them
contacts [person_added] = next_question
elif option == "Display all contacts":
print(contacts)
elif option == "Search":
search_question = input("Who are you looking for: ")
for search in contacts.items():
if search == search_question:
print("I found" + search)
else:
print("Contact not found")
elif option == "Delete contact":
person_deleted = input("Who would you like to be deleted ")
del(contacts[person_deleted])
else:
print("Thank You! Goodbye")
只需将
for i in range(0,1)
替换为 while(continue)
并在代码开头将变量 continue
定义为 True 怎么样?如果你这样做,它会继续,但 else 语句会变成
else:
print("Thank You! Goodbye")
continue = False