1

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:

  1. Does it mean that Python list items "index" can only be an integer (0,1,2)?
  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?"

4
  • Does it make sense to talk about the 2.59th item in a list? To map floats to other objects, you would use a dictionary. Commented Aug 7, 2019 at 2:29
  • 1a. Yes they can only be integers (positive ones). 1b. floats and integers serve different purposes depending on what you need. Specific calculations will usually use floats or doubles. Anything else an int Commented Aug 7, 2019 at 2:31
  • I don't understand, what are you trying to achieve with (for example) electronics[5.5][2], which element in the list do you want to access? A list just contains things. And those things can be counted. Like 1,2,3. What does 3.5 thing in a list even mean to you? Also, to answer your question. Python lists DO support float. You can add floats into your lists: [1.1, 2.3, 5.5] etc. Commented Aug 7, 2019 at 4:26
  • editindex used 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. Commented Aug 7, 2019 at 4:34

1 Answer 1

2

List indices must be integer from 0 up to n-1 where n is the length of the list. It also allows negative indexing within the range to access elements in the reverse direction.

List stores exactly n items and we only need those range of integer to access the data stored.

If you are given 1.0, you can always use int() to cast it to an int but if you end up with a float, you should ask the question, why would you end up with a float to access the data. Programming is more than mathematics in some sense, we want to do things that discourage bugs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.