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

Python字典删除功能失效求助:添加修改功能正常

问题:字典删除功能无法正常工作

我写了下面这段Python代码,添加、修改功能都正常,但删除功能一直用不了。试过pop、remove、del这些方法还是不行,求帮忙看看问题出在哪。

person = {"name": "Luca", "surname": "Rossi", "age": 25}

operations = ("adding", "modifying", "deleting", "read person", "exit")

def start():
     operation = input("What you wanna do? ")
     if operation == operations[0]:
         x = input("Add a key:value separated by a comma: ")
         add(x.split(","))
     elif operation == operations[1]:
         x = input("Select a key:value and modify it: ")
         modify(x.split(":"))
     elif operation == operations[2]:
         x = input("Select a key:value and delete it: ")
         delete(x)
     elif operation == operations[3]:
         print(person)
     elif operation == operation[4]:
         exit()
     

def add(param):
    key= param[0]
    value= param[1]
    person[key] = value
    print(person)

def modify(param):
    key = param[0]
    value = param[1]
    person[key] = value
    print(person)

def delete(param):
    key = param
    if key in person:
       del person[key]
       print(person)
    else:
       print(f"The key '{key}' does not exist.")


while True:        
   start()

问题分析与修复

你的删除功能失效主要有2个核心问题:

  1. 输入提示误导+未处理输入格式
    删除功能的输入提示写的是Select a key:value and delete it: ,会让你误以为要输入key:value格式,但delete函数只需要单独的键名。如果你输入了age:25,函数会把整个字符串当作键去匹配,而字典里根本没有这个键,自然删除失败。

  2. exit选项的判断错误
    代码里elif operation == operation[4]:是笔误,应该是operations[4],否则永远触发不了退出逻辑,但这和删除功能无关,顺便一起修正。

修复后的代码

person = {"name": "Luca", "surname": "Rossi", "age": 25}

operations = ("adding", "modifying", "deleting", "read person", "exit")

def start():
     operation = input("What you wanna do? ")
     if operation == operations[0]:
         x = input("Add a key:value separated by a comma: ")
         add(x.split(","))
     elif operation == operations[1]:
         x = input("Select a key:value and modify it: ")
         modify(x.split(":"))
     elif operation == operations[2]:
         # 修正提示,明确只需要输入键名
         x = input("Enter the key you want to delete: ")
         # 处理输入的空格,避免不小心输入空格导致匹配失败
         delete(x.strip())
     elif operation == operations[3]:
         print(person)
     # 修正exit的判断错误
     elif operation == operations[4]:
         exit()
     

def add(param):
    key= param[0].strip()  # 同样处理空格,添加时避免键带空格
    value= param[1].strip()
    person[key] = value
    print(person)

def modify(param):
    key = param[0].strip()
    value = param[1].strip()
    person[key] = value
    print(person)

def delete(param):
    key = param
    if key in person:
       del person[key]
       print(person)
    else:
       print(f"The key '{key}' does not exist.")


while True:        
   start()

额外优化(可选)

如果你担心自己还是会不小心输入key:value格式,可以给delete函数加个兼容处理:

def delete(param):
    # 如果输入了key:value格式,自动提取key
    key = param.split(":")[0].strip()
    if key in person:
       del person[key]
       print(person)
    else:
       print(f"The key '{key}' does not exist.")

这样不管你输入age还是age:25,都能正确匹配到键age进行删除。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 05:35:23