0

Why is my program showing index error while trying to take input a list without eval function? PYTHON

n = int(input("Enter no. of terms")) for a in range(0,n): l[a] = int(input("Enter value")) print(l)

3rd Oct 2021, 4:00 PM
Shan Kumar Singh
Shan Kumar Singh - avatar
3 Respostas
+ 4
Shan Kumar Singh Because list has 0 value so you can't assign value to a list like that. So do this: for a in range(0, n): l.append(int(input())) If you want to assign value like that then you have to define list with 0 value like this: n = int(input()) l = [0, 0] for a in range(0, n): l[a] = int(input()) print (l) If you enter 2 1 2 then list will be [1, 2]
3rd Oct 2021, 4:11 PM
A͢J
A͢J - avatar
+ 2
Hi Shan! I assume that l is an empty list. You just have to use append() method to add each element one by one to update list l. So, it needs to be like this n = int(input("Enter no. of terms")) l = [] for a in range(n): l.append(int(input("Enter value"))) print(l)
3rd Oct 2021, 5:46 PM
Python Learner
Python Learner - avatar
+ 2
Thank you both of you
4th Oct 2021, 1:56 AM
Shan Kumar Singh
Shan Kumar Singh - avatar