0

This is how the output should look like in the text file

Ray Holt 5 5 0 0 100 15 Jessica Jones 12 0 6 6 50 6 Johnny Rose 6 2 0 4 20 10 Gina Linetti 7 4 0 3 300 15 

The number is the result from the game that I have to create.

My question is, how can I write to the text file with both string and integer result ? I have tried this

def write_to_file(filename, player_list): output = open(filename, "w") for player in player_list: output.write(str(player)) 

but the output is

Ray Holt 5 5 0 0 100 15Jessica Jones 12 0 6 6 50 6Johnny Rose 6 2 0 4 20 10Gina Linetti 7 4 0 3 300 15Khang 0 0 0 0 100 0 

They are in 1 line

Please help me! Thanks a lot guys

5
  • 1
    Also write a '\n' somewhere… Commented Nov 2, 2019 at 8:19
  • You need to close the file afterwards- output.close() Commented Nov 2, 2019 at 8:20
  • stackoverflow.com/questions/11497376/… Commented Nov 2, 2019 at 8:21
  • Does this answer your question? Correct way to write line to file? Commented Nov 2, 2019 at 8:29
  • There is expected outcome described but nothing about how 'player_list' look like. Commented Nov 2, 2019 at 9:51

1 Answer 1

1

Use this:

def write_to_file(filename, player_list): output = open(filename, "w") for player in player_list: output.write(str(player)+'\n') output.close() 
Sign up to request clarification or add additional context in comments.

2 Comments

It doesnt work as I expect. The output should be a name line and then the number line then the name line and number line and so on
@KhangNguyen I don't know how player_list looks like. As such, I can't get a sample output. Please show at least some part of the list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.