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

求助:TypeError: list indices must be integers or slices, not str错误排查

错误分析与解决方案

错误原因

你遇到的TypeError: list indices must be integers or slices, not str由两处核心问题导致:

  • 循环变量名store_items覆盖了全局定义的商品价格字典,导致后续无法正确获取商品单价
  • 错误地尝试用商品编码(字符串类型)去索引列表container,列表仅支持整数或切片作为索引,因此触发类型错误

修正后的完整代码

# 打印店铺内所有药品
print("------------------------------------------------- 店铺商品 --------------------------------------------------")
print("编码 - 商品名称                                         |   编码 - 商品名称")
print("(B1) Advil Softget 200mg cap (Piece)            - 10.00  |  (10050) Allerta 10mg 2+1 tab (pack)        - 47.00  | ")
print("(B2) Alaxan FR cap (piece)                      - 9.00   |  (10051) Alnix drops 10ml syrup (bottle)    - 209.00 | ")
print("(B3) Allerkid 2.5mg drops 10 (bottle)           - 184.00 |  (10052) Alnix 5mg/60ml syrup (bottle)      - 300.00 | ")
print("(B4) Allerkid 5mg/30ml syrup (bottle)           - 168.00 |  (10053) Amlife 50mg/5mg tablet (piece)     - 26.00  | ")
print("(B5) 5mg/60ml syrup (bottle)                    - 294.00 |  (10054) Amlife 100mg/5mg tablet (piece)    - 31.00  |")
print("(B6) Alvedon 500mg tab (piece)                  - 5.00   |  (10055) Arcoxia 90mg tablet (piece)        - 70.00  |")
print("(B7) Appebon kid 120mlsyrup (bottle)            - 188.00 |  (10056) Arcoxia 120mg tablet (piece)       - 87.00  | ")
print("(B8) Asccof Ponakn 60ml syrup (bottle)          - 108.00 |  (10106) Alnix 10mg tab                     - 33.00  |")
print("(B9) Asferon 60ml syrup (bottle)                - 117.00 |  (10107) Ascof ponkan 60ml syrup (bottle)   - 108.00 |")
print("(B10) Aspilet 80mg tablet (piece)               - 4.00   |  (10108) Asferon gf 60ml syrup              - 117.00 | ")
print("(B11) Bioflu tab (piece)                        - 8.00   |  (10109) Azuthromycin-AZITERA 500mg tab     - 110.00 |")
print("(B12) Biogesic 15ml drops (bottle)              - 73.00  |  (10114) Ambroxol EXPEL 15ml drops (bottle) - 118.00 | ")
print("(B13) Biogesic 500mg tab (piece)                - 5.00   |  (10135) Betadine 10% 25ml soln (bottle)    - 98.00  | ")
print("(B14) Biogesic 500mg tab (piece)                - 5.00   |  (10262) Alaxan Tablet                      - 9.00   |")
print("(B15) Biogesic Orange 120mg 60ml syrup (bottle) - 86.00  |      ")
print("-----------------------------------------------------------------------------------------------------------------")
# 使用字典存储商品编码对应的价格
store_items = {"b1": 10.0, "b2": 9.0, "b3": 184.0, "b4": 168.0, "b5": 294.0, "b6": 5.0, "b7": 188.0, "b8": 108.0, "b9": 117.0, "b10": 4.0, "b11": 8.0,
     "b12": 73.0, "b13": 5.0, "b14": 5.0, "b15": 86.0, "10050": 47.0, "10051": 209.0, "10052": 300.0, "10053": 26.0, "10054": 31.0, "10055": 70.0,
     "10056": 87.0, "10106": 33.0, "10107": 108.0, "10108": 117.0, "10109": 110.0, "10114": 118.0, "10135": 98.0, "10262": 9.0}
# 初始化变量
container = []
# 定义收银流程函数
def cashier():
   user_select = input("请输入您想要购买的商品编码:").lower()
   while user_select != "quit":
       if user_select in store_items:
           container.append(user_select)
           user_select = input("商品已加入购物车,是否继续添加?(输入quit结束交易):").lower()
       else:
           user_select = input("无效编码,所选商品不在店铺列表中,是否继续添加?(输入quit结束交易):").lower()

cashier()
print("您选购的所有商品:{0}".format(container))
# 询问是否继续添加商品
retry = input("是否继续添加商品?(输入:yes/no):")
if retry.lower() == "yes":
   cashier()
   print("您选购的所有商品:{0}".format(container))

# 统一计算总价
total_amount_pay = sum(store_items[item] for item in container)

print("您需支付的总金额:", total_amount_pay)

关键修改点

  1. 修复变量覆盖问题:将循环变量名改为item,避免覆盖全局的store_items商品价格字典
  2. 修正索引错误:通过商品编码从store_items字典中获取对应价格,而非错误地索引container列表
  3. 优化代码结构:将总价计算逻辑提取为独立代码块,避免重复冗余;同时将retry判断改为小写统一比较,提升输入鲁棒性
  4. 修正字典笔误:原字典中102622应为10262,对应商品列表中的正确编码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 08:15:43