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

Python遍历字典列表打印消息出现AttributeError如何解决?

错误原因
  • 你的points变量是字典列表,属于list类型,只有字典类型才有items()方法用来遍历键值对,列表调用不存在的items()方法就会触发你遇到的AttributeError报错。
  • 你的遍历逻辑有误:需要先遍历列表拿到每个字典元素,再从字典中读取callsign、daily_points两个字段的值,不能直接对列表调用字典专属方法。
可正常运行的代码
points = [
    {"callsign": "A001", "daily_points":10, "number":"+447774076621"},
    {"callsign": "A002", "daily_points":5, "number":"+447958708481"}
]

# 先遍历列表取出每个字典元素
for item in points:
    print(f"Hi {item['callsign']} you received {item['daily_points']} points today.")
额外注意事项

你原代码里的输出字符串把received拼写错成了recieved,修正后输出内容才符合要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 06:30:00