my assignment code is working fine with an integer, but I realize that it cannot accept decimal and currency values for prices have decimals. Thinking I could just easily change all int input to float input, I got this error message. TypeError: list indices must be integers or slices, not float
The code snippet is for example:
electronics = [
["Sony cam","camera",199],["MS mouse","computer",299],
["ABC speakers","speaker",399]]
editinput = input("Select a product to edit. \n")
#somehow this cannot be float
editindex = float(editinput)-1
editprice = input("Price SGD $: \t")
# Changing editing an item in a nested list
electronics[editindex][2] = float(editprice)
Question:
- Does it mean that Python list items "index" can only be an integer (0,1,2)?
- Does this also mean that the common argument that there's nothing an int can do that a float can't, is no longer true, because list append, and edit is exactly where int is necessary and float can't.
Reference: Article number: 20452189 - "Why should I use ints instead of floats?"
editindexused in[..,]is an index, a location in a list. It has to be an integer. The item at that location can anything - a number, a string, another list, etc. You seem to be confusing value and index.