1

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. ​

dumb_guy
  • 81
  • 1
  • 12
  • can you directly reverse the input song list? an example here: https://stackoverflow.com/questions/742466/how-can-i-reverse-the-order-of-lines-in-a-file – Zhiya Apr 05 '18 at 15:25
  • That didn't work out in Python3, thanks for advise though. – dumb_guy Apr 05 '18 at 15:35
  • So you cannot change the file outside Python, with tools like unix shell? – Zhiya Apr 05 '18 at 16:11

3 Answers3

1

Read in the whole list, reverse it.

Remember where in the list you are.

Ask for how may to print.

Print one, advance position, continue until you reached the amount needed or the end is reached.

Repeat.

songs = """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"""


def getSongs():
    # """Read the file here and return it as list in the right order."""
    return songs.split("\n")[::-1] # return reversed list

allSongs = getSongs()
pos = 0   # position in the playlist
while True:
    N = int(input("How many more songs? "))
    for _ in range(N):
        print(allSongs[pos])     # print one
        pos += 1                 # advance
        if pos == len(allSongs): # more in list? continue else break from for
             break
    if pos == len(allSongs):     # more in list? continue else break from while
        break

Output:

How many more songs? 2
Hotel California, Eagles
Thriller, Michael Jackson
How many more songs? 3
Respect, Aretha Franklin
Piano Man, Billy Joel
Bohemian Rhapsody, Queen
How many more songs? 4
Creep, Radiohead
Total Eclipse of the Heart, Bonnie Tyler
American Pie, Don MacLean
Bohemian Rhapsody, Queen
How many more songs? 5
Hey Jude, The Beatles 
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks Patrick, you're the best. That worked out with your help and little modification. I appreciate you taking your time to read and solve this long question. – dumb_guy Apr 05 '18 at 16:18
0

All credit goes to Patrick Artner for his help, I am able to solve this one. I know this code can be furnished, however worked for me.
My code is:

songs = open('songlist.txt').read()

def getSongs():
    return songs.split("\n")[::-1] 

allSongs = getSongs()
pos = 1   
N = int(input("How many more songs? "))
for i in range(N):
        print(allSongs[pos])     
        pos += 1                 
        if pos == len(allSongs): 
             break
dumb_guy
  • 81
  • 1
  • 12
0

line = int(input('How many more songs? '))

songs = open('songlist.txt').read()

lista = songs.split("\n")[::-1]

i = 0 while i < line:

print(lista[i])

i += 1