首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修正这个“列表索引必须是整数或切片,而不是str”的TypeError?

如何修正这个“列表索引必须是整数或切片,而不是str”的TypeError?
EN

Stack Overflow用户
提问于 2022-07-24 21:30:11
回答 3查看 57关注 0票数 0

这是工作的代码:

代码语言:javascript
复制
candy_name = ["Assorted Small Lollipops", "Assorted Flavours Small", "Assorted Flavours Large", "Large Lollipop", "100g Assorted Flavours Small", "100g Assorted Flavours Large", "Candy Cane", "100g Candy Canes"]
candy_price = [0.1, 0.05, 0.2, 0.5, 4.5, 6, 0.2, 5.5]
candy_info = dict(zip(candy_name, candy_price))
candy_order, number_order, total = [], [], 0


while True:
    candy = input("which candy do you want?")

    while candy not in candy_info:
        candy = input("input error, try again:")
    candy_order.append(candy)
    number = input("How many %s would you like? " % candy)

    while not number.isdigit() or int(number) <= 0:
        number = input("Only integers greater than 0 are allowed, try again: ")
    number_order.append(int(number))

    keep_ordering = input("Would you like to add more to your order? y/n")
    while keep_ordering not in ["y", "n"]:
        keep_ordering = input("Simply enter y or n, try again:")

    if keep_ordering != "y":
        for _candy, _number in zip(candy_order, number_order):
            total += candy_info[_candy] * _number
        print("Your total spend is $%f" % total)
        break

但是,当我尝试在我自己的代码中实现它时,它不起作用,有人知道我做错了什么吗?我让它工作了几次,我不知道我是否改变了什么,使它不起作用

代码语言:javascript
复制
candy_name = ["Assorted Small Lollipops", "Assorted Flavours Small", "Assorted Flavours Large", "Large Lollipop", "100g Assorted Flavours Small", "100g Assorted Flavours Large", "Candy Cane", "100g Candy Canes"] #list with all candy types
candy_price = [0.1, 0.05, 0.2, 0.5, 4.5, 6, 0.2, 5.5] #all candy prices
total = 0
price_and_name = dict(zip(candy_name, candy_price))
candy_order = []
price_order = []
number_order = []

while True:
  for candies, price in zip(candy_name, candy_price):
    print(candies, "is $", price) #zips the two lists together so they line up

  price_and_name = [price_and_name.lower() for price_and_name in ["Assorted Small Lollipops", "Assorted Flavours Small", "Assorted Flavours Large", "Large Lollipop", "100g Assorted Flavours Small", "100g Assorted Flavours Large","Candy Cane", "100g Candy Canes"]] #makes the dictionary lowercase so the user input case doesn't matter
  
  candy = input("What candy would you like to order? ").lower()

  while candy not in price_and_name:
    candy = input("That was not one of the options, check your spelling and try again. ").lower()
  candy_order.append(candy)
  number = input("How many %s would you like? " % candy)

  while not number.isdigit() or int(number) <= 0:
    number = input("Only integers greater than 0 are allowed, please try again.")
  number_order.append(int(number))

  keep_ordering = input("Would you like to add more to your order? ").lower()
  while keep_ordering not in ["yes", "no"]:
    keep_ordering = input("Please enter just yes or no, try again. ").lower()

  if keep_ordering != "yes":
    for _candy, _number in zip(candy_order, number_order):
      total += price_and_name[_candy] + _number
    print("Your total is $%f" % total)
    break

以下是完整的错误消息:全错误信息

谢谢你的帮助。

EN

回答 3

Stack Overflow用户

发布于 2022-07-24 21:51:51

得到该错误的原因是,price_and_name变量是一个列表,您可能认为它是一个字典。您最初创建的字典在这里被列表所取代:

代码语言:javascript
复制
price_and_name = [price_and_name.lower() for price_and_name in ["Assorted Small Lollipops", "Assorted Flavours Small", "Assorted Flavours Large", "Large Lollipop", "100g Assorted Flavours Small", "100g Assorted Flavours Large","Candy Cane", "100g Candy Canes"]] #makes the dictionary lowercase so the user input case doesn't matter

这一行不使字典小写,而是用小写candy_names列表替换它。如果您希望candy_names是小写的,那么只需调用

代码语言:javascript
复制
candy_names.lower()

声明它之后,然后创建的字典将小写candy_names作为键保存。

票数 1
EN

Stack Overflow用户

发布于 2022-07-24 21:43:42

candy_order是一个包含字符串值的列表。

代码语言:javascript
复制
    if keep_ordering != "yes":
        for _candy, _number in zip(candy_order, number_order):
          # _candy: string
          # _number: int
          total += price_and_name[_candy] + _number
          # you are trying to index price_and_name using _candy which is a string
        print("Your total is $%f" % total)
    break
票数 0
EN

Stack Overflow用户

发布于 2022-07-24 21:54:29

在尝试更新price_and_name以包含糖果名称的小写版本时,您将其从dict更改为list (正如@mozway的注释中指出的那样),因此当您稍后尝试通过str键访问dict值时,它会给出问题中的错误。

另外,在不相关的情况下,total的计算应该使用乘法,而不是加法。

建议的更改:

如果您更改这一行:

代码语言:javascript
复制
price_and_name = dict(zip(candy_name, candy_price))

..。是这样的:

代码语言:javascript
复制
price_and_name = dict(zip([x.lower() for x in candy_name], candy_price))

..。并删除这一行:

代码语言:javascript
复制
  price_and_name = [price_and_name.lower() for price_and_name in ["Assorted Small Lollipops", "Assorted Flavours Small", "Assorted Flavours Large", "Large Lollipop", "100g Assorted Flavours Small", "100g Assorted Flavours Large","Candy Cane", "100g Candy Canes"]] #makes the dictionary lowercase so the user input case doesn't matter

..。并更改这一行:

代码语言:javascript
复制
      total += price_and_name[_candy] + _number

..。对此:

代码语言:javascript
复制
      total += price_and_name[_candy] * _number

..。看起来不错。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73102188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档