如何修复此类型错误"list indices must be integers or slices, not str"?



这是有效的代码:

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

然而,这很好用,当我试图在自己的代码中实现它时,它不起作用,有人知道我做错了什么吗?我让它工作了几次,我不确定我是否改变了什么,使它不工作

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

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

谢谢你的帮助。

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

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_name列表替换它。如果您希望candy_name是小写的,那么只需调用

candy_names.lower()

在声明它之后,然后创建的字典将使用小写的candynames作为它的键。

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


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

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

此外,在一个无关的注意事项上,total的计算应该使用乘法,而不是加法。

建议更改:

如果您更改此行:

price_and_name = dict(zip(candy_name, candy_price))

是这样的:

price_and_name = dict(zip([x.lower() for x in candy_name], candy_price))

并删除此行:

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

并更改此行:

total += price_and_name[_candy] + _number

到此:

total += price_and_name[_candy] * _number

它会起作用的。

相关内容

  • 没有找到相关文章

最新更新