Python 我得到“要解包的多个值出错,期望值为1”
当我使用search if语句时,我得到一个错误。要解压缩的多个值应为1。我该怎么做我已经试着改变了。谢谢你的建议。我刚开始编码,所以请对我放松点。谢谢Python 我得到“要解包的多个值出错,期望值为1”,python,syntax-error,Python,Syntax Error,当我使用search if语句时,我得到一个错误。要解压缩的多个值应为1。我该怎么做我已经试着改变了。谢谢你的建议。我刚开始编码,所以请对我放松点。谢谢 #Map contacts = {} #Intro print("Address book to store friends contact") print("-" * 50) print("-" * 50) while True: #Display of options print("Select an option
#Map
contacts = {}
#Intro
print("Address book to store friends contact")
print("-" * 50)
print("-" * 50)
while True:
#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: ")
#Main program
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)
print("-" * 50)
print(" " * 50)
elif option == "Search":
search_question = str(input("Who are you looking for: "))
for search in contacts.items():
if search == str(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])
print("I just deleted " + person_deleted)
else:
print("Thank You for using ME! Goodbye")
break
这里有一个问题。.items方法返回成对的键和值,因此在第一个print语句中向字符串添加搜索将导致问题。此外,代码有点低效,我们还可以解决:
search_question = input("Who are you looking for: ")
if search_question in contacts:
print("I found" + search_question)
else:
print("Contact not found")
input已经返回了一个字符串,因此我们不需要尝试使用str来转换它。此外,如果b是字典,那么b中的a将返回a是否是b的键,这正是我们所希望的行为。如果您在此处发布完整错误,将有所帮助。Python错误提供了很多信息,比如在contacts.items中搜索时出现错误的确切行和位置:-是有问题的行。items返回一对密钥,值请将完整的错误回溯添加到您的问题中!我知道了谢谢你的帮助!请发布你的答案,没有什么比发现有同样问题的人在没有答案的情况下说“我解决了”更令人沮丧的了
search_question = input("Who are you looking for: ")
if search_question in contacts:
print("I found" + search_question)
else:
print("Contact not found")