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)
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]
+ 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)
+ 2
Thank you both of you