Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'float' object is not subscriptable

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?

like image 529
oreid Avatar asked Nov 15 '13 01:11

oreid


People also ask

Are floats Subscriptable?

Non subscriptable objects are those whose items can't be accessed using index numbers. Example float, int, etc. Examples of subscriptable objects are strings, lists, dictionaries.

How do you fix an object is not Subscriptable error?

Python throws the TypeError object is not subscriptable if you use indexing with the square bracket notation on an object that is not indexable. This is the case if the object doesn't define the __getitem__() method. You can fix it by removing the indexing call or defining the __getitem__ method.

What does type object is not Subscriptable mean in Python?

The “TypeError: 'type' object is not subscriptable” error is raised when you try to access an object using indexing whose data type is “type”. To solve this error, ensure you only try to access iterable objects, like tuples and strings, using indexing.

What is a float in Python?

Float() is a method that returns a floating-point number for a provided number or string. Float() returns the value based on the argument or parameter value that is being passed to it. If no value or blank parameter is passed, it will return the values 0.0 as the floating-point output.


3 Answers

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 
like image 162
Joshua Avatar answered Sep 18 '22 13:09

Joshua


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 
like image 33
roippi Avatar answered Sep 17 '22 13:09

roippi


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)
like image 21
inspectorG4dget Avatar answered Sep 20 '22 13:09

inspectorG4dget