keirbtre keirbtre - 2 months ago 16
Python Question

Storing string inputs in a list

I've been given a small task - to get the user to input a limerick, and for the program to store each line in a list as the line is entered. (I then have to print the limerick back)
This is my current code:

limerick_line = []
for i in range(5):
limerick_line[i].append = input("Type in a limerick line. ")
print(limerick_line[5])


I know I'm doing quite a lot wrong, but I'm not exactly sure how to get it right. The current error is "list index out of range".

Answer Source

You just need to use limerick_line.append(input('enter line: ')) - no need to try and index it... And your final print can be just print(limerick_line) or loop over it:

for line in limerick_line:
    print(line)

So, corrected code is:

limerick_line = []
for i in range(5):
    limerick_line.append( input("Type in a limerick line. ") )
for line in limerick_line:
    print(line)