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)
Share:
301,751
oreid
Author by

oreid

Senior Software Engineer at Zendesk in Melbourne, Australia.

Updated on July 07, 2020

Comments

  • oreid
    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
    roippi over 10 years
    a slice from 0 to 7 has 7 elements, not 6.
  • oreid
    oreid over 10 years
    The 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
    Pruthvikar over 10 years
    see my edit above, just multiply by the number of items you are replacing
  • oreid
    oreid over 10 years
    Thank you very much gentlemen.