Python 类型错误:“浮动”对象不可下标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19991591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:11:34  来源:igfitidea点击:

TypeError: 'float' object is not subscriptable

pythonlistreplaceinsertindexing

提问by beardo

PizzaChange=float(input("What would you like the new price for all standard pizzas to be? "))      
PriceList[0][1][2][3][4][5][6]=[PizzaChange]  
PriceList[7][8][9][10][11]=[PizzaChange+3]

Basically I have an input that a user will put a number values (float input) into, then it will set all of these aforementioned list indexes to that value. For some reason I can't get it to set them without coming up with a:

基本上我有一个输入,用户将把一个数字值(浮点输入)放入其中,然后它将所有这些上述列表索引设置为该值。出于某种原因,我无法在不提出以下问题的情况下设置它们:

TypeError: 'float' object is not subscriptable

error. Am I doing something wrong or am I just looking at it the wrong way?

错误。是我做错了什么还是我只是用错误的方式看待它?

采纳答案by Joshua

PriceList[0]is a float. PriceList[0][1]is trying to access the first element of a float. Instead, do

PriceList[0]是一个浮点数。PriceList[0][1]正在尝试访问浮点数的第一个元素。相反,做

PriceList[0] = PriceList[1] = ...code omitted... = PriceList[6] = PizzaChange

or

或者

PriceList[0:7] = [PizzaChange]*7

回答by Pruthvikar

You are not selecting multiple indexes with PriceList[0][1][2][3][4][5][6] , instead each [] is going into a sub index.

您没有使用 PriceList[0][1][2][3][4][5][6] 选择多个索引,而是每个 [] 都进入一个子索引。

Try this

尝试这个

PizzaChange=float(input("What would you like the new price for all standard pizzas to be? "))      
PriceList[0:7]=[PizzaChange]*7  
PriceList[7:11]=[PizzaChange+3]*4

回答by roippi

PriceList[0][1][2][3][4][5][6]

This says: go to the 1st item of my collection PriceList. That thing is a collection; get its 2nd item. That thing is a collection; get its 3rd...

这说:转到我收藏的第一个项目PriceList。那东西是一个集合;得到它的第二个项目。那东西是一个集合;得到它的第三...

Instead, you want slicing:

相反,你想要切片

PriceList[:7] = [PizzaChange]*7

回答by inspectorG4dget

PizzaChange=float(input("What would you like the new price for all standard pizzas to be? "))      
for i,price in enumerate(PriceList):
  PriceList[i] = PizzaChange + 3*int(i>=7)

回答by Pi Marillion

It looks like you are trying to set elements 0 through 11 of PriceList to new values. The syntax would usually look like this:

看起来您正在尝试将 PriceList 的元素 0 到 11 设置为新值。语法通常如下所示:

prompt = "What would you like the new price for all standard pizzas to be? "
PizzaChange = float(input(prompt))
for i in [0, 1, 2, 3, 4, 5, 6]: PriceList[i] = PizzaChange
for i in [7, 8, 9, 10, 11]: PriceList[i] = PizzaChange + 3

If they are always consecutive ranges, then it's even simpler to write:

如果它们总是连续的范围,那么写起来就更简单了:

prompt = "What would you like the new price for all standard pizzas to be? "
PizzaChange = float(input(prompt))
for i in range(0, 7): PriceList[i] = PizzaChange
for i in range(7, 12): PriceList[i] = PizzaChange + 3

For reference, PriceList[0][1][2][3][4][5][6]refers to "Element 6 of element 5 of element 4 of element 3 of element 2 of element 1 of element 0 of PriceList. Put another way, it's the same as ((((((PriceList[0])[1])[2])[3])[4])[5])[6].

作为参考,PriceList[0][1][2][3][4][5][6]参考“元素0的元素1的元素2的元素3的元素3的元素5的元素6的元素PriceList。换句话说,它与((((((PriceList[0])[1])[2])[3])[4])[5])[6]