Home python Problem with array input

Problem with array input

Author

Date

Category

The code does not compile. I can’t figure out why the error occurs.

Line 6 problem:

spisok[i] = input()

IndexError: list assignment index out of range

spisok = list()
print('Determine the number of students in your group.')
KolStud= int(input())
for i in range(KolStud):
    print('Enter your full name['+str(i)+']: ')
    spisok[i] = input()
print(spisok)

Answer 1, authority 100%

as @S already pointed out. Nick, the error is that you are trying to pass an input value to array elements that do not yet exist, use the construction instead:

.append()

which adds elements to the end of the list, or, if the size of the array is known, then you can create an empty array for a specified number of elements, and then fill it.

spisok = [0] * KolStud

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions