1370607278 发表于 2022-3-9 12:29:31

python字典里pop的问题

print("---欢迎进入通讯录程序---\
      \n---1:查询联系人资料---\
      \n---2:插入新的联系人---\
      \n---3:删除已有联系人---\
      \n---4:退出通讯录程序---")
contacts = {}
temp = int(input("请输入相关的指令代码:"))
i = 1
while i == 1:
    if temp == 1 :
      names = input("请输入联系人姓名:")
      if names not in contacts :
            print("未查询到该联系人")
            temp = int(input("请输入相关的指令代码:"))
      else:
            print(names,contacts,end="")
            temp = int(input("\n请输入相关的指令代码:"))
    if temp == 2 :
      names = input("请输入联系人姓名:")
      if names in contacts :
            print("您输入的姓名在通讯录中已存在 -->> ",names ,":", tel,end = "")
            temp_1 = input("\n是否修改用户资料(YES/NO): ")
            if temp_1 == "YES":
                temp_2 = input("请输入用户的联系电话:")
                contacts = temp_2
                temp = int(input("\n请输入相关的指令代码:"))
            else:
                temp = int(input("\n请输入相关的指令代码:"))
      else:
            tel = input("请输入用户联系电话:")
            contacts = tel
            print("已成功存储联系人:",names,"-->>",tel,end = "")
            temp = int(input("\n请输入相关的指令代码:"))

    if temp == 3 :
      names = input("请输入联系人姓名:")
      contacts.pop(names , "未找到该联系人")
      temp = int(input("\n请输入相关的指令代码:"))
    if temp == 4 :
      i = 2
      print("---感谢使用通讯录程序---")




问题:
1.对于contacts.pop(names , "未找到该联系人")这个代码,为什么输出names不在contacts里的时候并不输出:未找到该联系人这句话,我记得pop函数不是如果names不存在的话会返回逗号后面的代码吗?

为什么我在输出的时候并没有这样显示

2.这个代码有没有可以改进的地方,或者是否还存在bug! 谢谢大佬们

2012277033 发表于 2022-3-9 23:22:18

没打印是因为你没接收,那一句改成这样就行了
print(contacts.pop(names , "未找到该联系人"))

甲鱼python 发表于 2022-3-10 09:34:14

你没print输出 当然没有

甲鱼python 发表于 2022-3-10 10:08:53

print("---欢迎进入通讯录程序---\
      \n---1:查询联系人资料---\
      \n---2:插入新的联系人---\
      \n---3:删除已有联系人---\
      \n---4:退出通讯录程序---")
contacts = {}
temp = int(input("请输入相关的指令代码:"))
# 后面的if都换成elif执行效率高点
while True:
    if temp == 1 :
      names = input("请输入联系人姓名:")
      if names not in contacts :
            print("未查询到该联系人")
            temp = int(input("请输入相关的指令代码:"))
      else:
            print(names,contacts,end="")
            temp = int(input("\n请输入相关的指令代码:"))
    elif temp == 2 :
      names = input("请输入联系人姓名:")
      if names in contacts :
            print("您输入的姓名在通讯录中已存在 -->> ",names ,":", tel,end = "")
            temp_1 = input("\n是否修改用户资料(YES/NO): ")
            if temp_1.upper() == "YES":# 用户可能输入小写,都转换为大写在判断
                temp_2 = input("请输入用户的联系电话:")
                contacts = temp_2
                temp = int(input("请输入相关的指令代码:"))
            else:
                temp = int(input("请输入相关的指令代码:"))
      else:
            tel = input("请输入用户联系电话:")
            contacts = tel
            print("已成功存储联系人:",names,"-->>",tel,end = "")
            temp = int(input("\n请输入相关的指令代码:"))

    elif temp == 3 :
      names = input("请输入联系人姓名:")
      print(contacts.pop(names , "未找到该联系人"), end = "")# 用print输出
      temp = int(input("\n请输入相关的指令代码:"))
    elif temp == 4 :
      print("---感谢使用通讯录程序---")
      break # 用break跳出循环 不用i

1370607278 发表于 2022-3-10 12:10:01

甲鱼python 发表于 2022-3-10 10:08


明白了,谢谢哥!
页: [1]
查看完整版本: python字典里pop的问题