You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python通讯录程序添加功能失效问题排查求助

Python通讯录程序问题分析与修复

问题原因

  • 分支逻辑错误:elif what_to_do == "Add" 与elif email_numb == "M"同级,输入M选择Add后,程序不会立即执行添加流程,而是直接走到stop_or_continue环节。下一次循环时,如果输入的内容不匹配Email/Phone/M,才会触发这个分支,导致随机输入时误进入添加步骤。
  • 字典更新方式错误:emails.update({new_pair}) 把姓名和邮箱用冒号拼接成字符串作为参数,而update需要传入字典格式{键: 值},这样的写法无法正确添加键值对到字典中。
  • 无效条件判断:elif email_numb != {'Email', 'Phone', "M"} 是拿字符串和集合做不等于比较,这个条件永远为True,会覆盖后续的分支逻辑,导致错误提示逻辑混乱。
  • 变量未初始化风险:what_to_do仅在输入M时才会被定义,其他情况下访问该变量会抛出NameError。

修复后的代码

from time import sleep

emergency = "911"
phone = {"Mother":"xxx-xxx-xxxx", "Father":"xxx-xxx-xxxx", "911":emergency,
"Police":emergency, "Fire":emergency, "Emergency":emergency}

emails = {"Mother":"example@gmail.com", "Father":"example@gmail.com",
"911":emergency, "Police":emergency, "Fire":emergency, "Emergency":emergency}


while True:
    email_numb = input("Do you want Email or Phone numbers? (Type one, Phone or Email) For more options type m: ").lower().capitalize()
    # 初始化变量避免未定义报错
    what_to_do = ""

    if email_numb == "Email":
        email_input = input("Who's email would you like to find? (enter a name): ").lower().capitalize()
        if email_input in emails:
            print(emails[email_input])
        else:
           print("Sorry but I cannot find the name, check out for typos")
        sleep(2)

    elif email_numb == "Phone":
        phone_input = input("Who's phone number would you like to find? (enter a name): ").lower().capitalize()
        if phone_input in phone:
            print(phone[phone_input])
        else:
            print("Sorry but I cannot find the name, check for typos")
        sleep(2)
    
    elif email_numb == "M":
       what_to_do = input("Do want to edit, add, or delete, a person in the directory? (Type add, edit, or delete) ").lower().capitalize()
       
       # 将Add分支嵌套在M选项内,选择后立即执行对应逻辑
       if what_to_do == "Add":
            email_number = input("Would you like to add a new email address or a new phone number? (type email or phone) ").lower().capitalize()
            if email_number == "Email":
                name = input("Please type the name: ").lower().capitalize()
                email = input("Please type the email: ")
                emails[name] = email
                print(f"Added email for {name}: {email}")
            elif email_number == "Phone":
                name = input("Please type the name: ").lower().capitalize()
                phone_num = input("Please type the phone number: ")
                phone[name] = phone_num
                print(f"Added phone number for {name}: {phone_num}")
            else:
                print("Invalid option, please type email or phone")
            sleep(2)
       # 预留edit和delete功能框架
       elif what_to_do == "Edit":
            print("Edit feature not implemented yet")
            sleep(2)
       elif what_to_do == "Delete":
            print("Delete feature not implemented yet")
            sleep(2)
       else:
            print("Sorry, I don't understand the option")
            sleep(2)

    else:
        # 统一处理不匹配的输入
        print("Sorry, I don't understand")
        sleep(2)
        

    stop_or_continue = input("Would you like to search up another number/email address? Press q to quit and any other key to continue: ")

    if stop_or_continue.lower() == "q":
        break
    else:
        continue

关键修改说明

  • 将Add分支嵌套在email_numb == "M"的条件块内,选择Add后立即执行添加流程,不会跳转到后续的stop_or_continue。
  • 修复字典更新逻辑:直接使用emails[name] = email或phone[name] = phone_num添加新键值对,替代错误的update用法。
  • 初始化what_to_do变量,避免未定义的报错。
  • 用else统一处理不匹配的输入,替代原无效的集合比较条件,逻辑更清晰。
  • 补充添加电话号码的逻辑,完善功能完整性。
  • 对输入姓名统一做大小写格式化,保持字典内键的格式一致性。

内容的提问来源于stack exchange,提问作者Aharon Becker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 12:57:16