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?
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.
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.
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.
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.
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
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With