-1

I am trying to make a program that will put the output of the user into a text file. What I want to do is have the text printed on multiple lines, not the same line. For example, if the UserName variable is set to "User":

UserName = input('What is your name?: ')


with open('Results ' + UserName, 'w') as WriteFile:
    WriteFile.write('Results for ' + UserName)


with open('Results ' + UserName, 'a') as WriteFile:
    WriteFile.write(' If this text is shown on another line, it means this was a success.')

The output would be:

Results for User If this text is shown on another line, it means this was a success.

How could I make the output say:

Results for User

If this text is shown on another line, it means this was a success.

Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
  • 2
    You insert a newline character, `\n`. Where are you stuck? – Prune Nov 04 '20 at 00:51
  • Hello! I voted to close this question because the solution seems pretty minor, I couldn't find a suitable duplicate but the info you needed is findable for instance [this thread](https://stackoverflow.com/questions/21019942/write-multiple-lines-in-a-file-in-python) has it being done in the question. – Tadhg McDonald-Jensen Nov 04 '20 at 01:42

1 Answers1

2
UserName = input('What is your name?: ')

with open('Results ' + UserName, 'w') as WriteFile:
    WriteFile.write('Results for ' + UserName + "\n"+ "\n"+ 
         'If this text is shown on another line, it means this was a success.')

You can add a newline to your file by using '\n'.

Dharman
  • 30,962
  • 25
  • 85
  • 135