Ask coding questions

← Back to all posts
Add to a list based on `input()`
crazyCoder691 (8)

I have a list called to_calc
I want to add to the list like this:

index = 0
to_calc = []
while True:
  to_calc[index] = float(input())
  index += 1

Obviously, it is giving me an "out-of-range" error.
I want to be able to ask this an infinite amount of times until the user wants to stop adding and calculate, which I will make code for. So filling up the list with "null", "null", "null",... won't work. It will either go out of range or be filled up with "null"'s by the time the user is done. Please help

Answered by XanderEhlert (134) [earned 5 cycles]
View Answer
XanderEhlert (134)

try using to_calc.append(float(input()))

your syntax is kind of how keys and values are "appended" to dictionaries

crazyCoder691 (8)

Thank you this worked!

ronniegertman (4)

you should use append() or
to_calc += float(input())