I need to write a python program that reads in a text file (songlist.txt - which has list of songs stored in a text file reverse order) and how many more songs will be sung. Then it prints song list in the correct order.
songlist.txt reads like following:
Hey Jude, The Beatles
Bohemian Rhapsody, Queen
American Pie, Don MacLean
Total Eclipse of the Heart, Bonnie Tyler
Creep, Radiohead
Bohemian Rhapsody, Queen
Piano Man, Billy Joel
Respect, Aretha Franklin
Thriller, Michael Jackson
Hotel California, Eagles
I am able to either print complete songlist in correct order(reverse of file songlist.txt) with this code
for line in reversed(list(open("songlist.txt"))):
print(line.rstrip())
or print songs upto N lines (incorrect order - because it's not reversed) with the following code:
N = int(input("How many more songs? "))
file = open('songlist.txt', 'r')
for i in range(1,N+1):
A = file.readline()
print(A)
However I am unable to join these two codes to make it work as intended; print upto N lines in correct order (means print song list like follwong):
My program should work like the following examples:
How many more songs? 2
Hotel California, Eagles
Thriller, Michael Jackson
Next example:
How many more songs? 4
Hotel California, Eagles
Thriller, Michael Jackson
Respect, Aretha Franklin
Piano Man, Billy Joel
Question might be too long, however, I'm stuck here, thanks for your inputs.