Python 3.x Python中的字典无法正确打印

Python 3.x Python中的字典无法正确打印,python-3.x,dictionary,Python 3.x,Dictionary,我想打印联系人地图,但它不起作用 我删除了所有的if语句,它工作了,但是一旦我放入if语句,它就失败了 while True: contacts = {} print('''Type 1 to Add/Update contact 2 to Display all contacts 3 to Search 4 to Delete contact 5 to Quit.''') choice = input("Which option

我想打印联系人地图,但它不起作用

我删除了所有的if语句,它工作了,但是一旦我放入if语句,它就失败了

while True:
    contacts = {}
    print('''Type

    1 to Add/Update contact
    2 to Display all contacts
    3 to Search
    4 to Delete contact
    5 to Quit.''')
    choice = input("Which option?")
    if int(choice) == 1:
        contacts = {}
        name = input("Enter the name of the contact.")
        contact = input("Enter the phone number/email address.")
        contacts[name] = contact
    if int(choice) == 2:
        print("Done!")
        print(contacts)
    if int(choice) == 3:
        for key, value in contacts.items():
            print(key)
输出只是{}。

在我看来是正确的。 也许试试

mycontact=contacts.keys()
phone=contacts.values()
print(mycontact)
print(phone)
若我的解决方案和你们的解决方案都不起作用,那个么试着仔细检查你们的If语句

choice = input("Which option?")
if int(choice) == 3:
    print(this works)
else
    print(this does not work)

在while循环的每个迭代中,您都在清除联系人字典

while True:
    contacts = {}
    ...
应该是

contacts = {}
while True:
...

对不起,我说得太模糊了。我的意思是intchoice==1,当我输入姓名和联系人时,例如name=bob和contact=911,当我输入intchoice==2时,打印出来的是{},而不是{bob,911}。