-2

Im trying to do a line break for every single number I send to "stuff.txt"

here's my code...

import dice # dice is a module i used

d = 1
stuff = open("stuff.txt", "a")
while d <= 100:
    print(d)
    d = d + 1
    stuff.write(str(dice.roll_dice(4)  ))
    print("sent a random number from 1-4 to stuff.txt")
stuff.close()

for the dice module

import random


def roll_dice(num):
    return random.randint(1, num)

When I run the code, I get 100 dice rolls but all the numbers are all on the same line. What i'm trying to do is have those numbers on seperate lines using a line break. I want the numbers to turn into:

1
2
3

instead of 123

muw
  • 9
  • 3
  • 1
    Voting to close as duplicate of [Writing string to a file on a new line every time](https://stackoverflow.com/questions/2918362/writing-string-to-a-file-on-a-new-line-every-time) – esqew Apr 15 '21 at 03:39

1 Answers1

-1

Just add \n (the newline character) after each. That is:

stuff.write(str(dice.roll_dice(4)  )+"\n")
Cole Henrich
  • 155
  • 3
  • 17