TypeError: 'float' object is not subscriptable
301,751
Solution 1
PriceList[0]
is a float. PriceList[0][1]
is trying to access the first element of a float. Instead, do
PriceList[0] = PriceList[1] = ...code omitted... = PriceList[6] = PizzaChange
or
PriceList[0:7] = [PizzaChange]*7
Solution 2
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...
Instead, you want slicing:
PriceList[:7] = [PizzaChange]*7
Solution 3
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)
Author by
oreid
Senior Software Engineer at Zendesk in Melbourne, Australia.
Updated on July 07, 2020Comments
-
oreid almost 4 years
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?
-
roippi over 10 yearsa slice from 0 to 7 has 7 elements, not 6.
-
oreid over 10 yearsThe problem with this is it replaces those index with only one value. For example I want List=[1,2,3,4,5,6,7,8,9,10,11] to eqaul when I put say 3 in my input List=[3,3,3,3,3,3,3,7,8,9,10,11].
-
Pruthvikar over 10 yearssee my edit above, just multiply by the number of items you are replacing
-
oreid over 10 yearsThank you very much gentlemen.