1

okay, so im trying to write a program that takes a list of numbers 1 to 1000, reverses them, puts them into a file (my_file), and prints them back into the file. so far, my code looks like this:

new_list = list(range(1001)) my_file = open("text.txt", "w") for x in (reversed(new_list)): my_file.write(x)

And, I know im doing something wrong, im just not sure what (although it probably has to do with the last line of coding?)

PS, sorry if this question is worded awfully, this is my first time using stackoverflow to ask a question :)

BlankPaige
  • 23
  • 2
  • Just replace `my_file.write(x)` by `my_file.write('%d\n' %x)`. You need to define the format while writing. The newline character `\n` writes every number in a new line in the file. Also, put `my_file.close()` after the `for` loop. – Sheldore Aug 03 '18 at 00:42
  • Alternatively, you can also use `my_file.write('{}\n'.format(x))` – Sheldore Aug 03 '18 at 00:48
  • `x` is an `int` so you'd need to `str(x)` to be able to print it. You'll also need to `print("\n")` to put a new line in. – sniperd Aug 03 '18 at 12:43

0 Answers0