Help with adding 2.5 to 2d list

pizza_and_prices.insert(2.5, “peppers”)
TypeError: integer argument expected, got float

How am I supposed to write this so it lets me use 2.5?

Hi welcome to the forums :slight_smile:

The error is as it says, it expects an integer.

  • If the error is point at where you call your function, then it suggests that your function declaration explicitly asks for ints. If you want to use doubles/floats, adjust the declaration.
  • However since we don’t have any info about where you error is talking about, it could mean it’s unrelated, and at some point you try do something that’s reserved for ints.

More information is needed: What is the entire error code (as in, what line is it pointing to)? How did you declare your function? Can you link to the exercise/the rest of you code?

(also please format for when you do these :slight_smile: : [How to] Format code in posts)

# Your code below:
toppings = ["pepperoni", "pineapple", "cheese", "sausage", "olives", "anchovies", "mushrooms"]

prices = [2, 6, 1, 3, 2, 7, 2]
num_two_dollar_slices = prices.count(2)
print(num_two_dollar_slices)

#step 4
num_pizzas = len(toppings)
print(num_pizzas)
print("We sell", num_pizzas, "different kinds of pizza!")

pizza_and_prices = [
  [2, "pepporoni"],
  [6, "pineapple"],
  [1, "cheese"],
  [3, "sausage"],
  [2, "olives"],
  [7, "anchovies"],
  [2, "mushrooms"]
]

pizza_and_prices.sort()
cheapest_pizza = pizza_and_prices[0]


#number 10
priciest_pizza = pizza_and_prices[-1]
#11
pizza_and_prices.pop(-1)
pizza_and_prices.insert(2.5, "peppers")

Traceback (most recent call last):
File “script.py”, line 31, in
pizza_and_prices.insert(2.5, “peppers”)
TypeError: integer argument expected, got float

I didn’t notice you were using an insert method. (d’oh! :sweat_smile:)
Insert method takes an index and inserts an item in that index. By definition, you can’t have non-integer indices, hence the error.

If you want to use insert (instead of append), you would have to point to the index you want to insert it (if order matters to you) and insert the entire object you want. If order doesn’t matter I recommend using append() and just add it to the end of your array.

In the “help” part it tells me to use the insert method and sort it. I was just trying to figure out how to add the non-integer before trying to sort it. Sorry I’m very new to this but what do you mean by point to the index you want to insert it.

Here’s an example from the terminal:

>>> my_list = [9,10,12]
>>> my_list 
[9, 10, 12]
>>> my_list.insert(2, 11)
>>> my_list
[9, 10, 11, 12]

I suggest playing around with it in a terminal (you can use one online if you’re on windows, just go here: Welcome to Python.org)

1 Like

but looking at your structure:

pizza_and_prices = [
  [2, "pepporoni"],
  [6, "pineapple"],
  [1, "cheese"],
  [3, "sausage"],
  [2, "olives"],
  [7, "anchovies"],
  [2, "mushrooms"]
]

you have a nested list, so regardless of which method (append or insert) you use, you want to add a list:

example.insert(index, [float_value, name])
3 Likes

pizza_and_prices.insert(3, [2.5, “peppers”])

print(pizza_and_prices)

pizza_and_prices.sort()

i did this and it places it where it should be