Brian Fuller
Brian Fuller

Reputation: 416

Enter each line user inputs with file. functions

n = int(input('How many tracks are in the album?: ')) 
for i in range(n): 
    line = raw_input('Next Track: ') 
    lines.append(line) 

Where it says line = raw_input('Next Track: ') is where the text would be saved to the file. But, if there are, say, 20, how would you make it so each track is recorded and saved?

Here is the code where the text gets written:

f.write("Track Name/Rating: " + line +"\n")

Upvotes: 0

Views: 50

Answers (1)

Ghostwriter
Ghostwriter

Reputation: 2521

I think that your write() function should be inside the for loop. Appending line to lines list is not neccessary then. You just write every line "on fly" to the file. Remember to define f before with "w" parameter and to add f.close or your line will get stuck in a buffer.

Here is a working piece of code:

n = int(input('How many tracks are in the album?: '))


f=open("directory","w")

for i in range(n):

    line = raw_input('Next Track: ') 
    f.write("Track Name/Rating: " + line +"\n")
f.close()

Upvotes: 1

Related Questions