Reputation: 33
Im trying to create a random number generator that writes to a text file. The full code will execute perfectly with th exception it only executes 1 number. I need it to be 12. I also know that if i take out the code that produces the the 12 numbers using ap rint command, but as soon as i insert it back in without the print command and try and send it to a txt file it goes back to only doing 1.
#This program writes 1 line of 12 random integers, each in the
#range from 1-100 to a text file.
def main():
import random
#Open a file named numbersmake.txt.
outfile = open('numbersmake.txt', 'w')
#Produce the numbers
for count in range(12):
#Get a random number.
num = random.randint(1, 100)
#Write 12 random intergers in the range of 1-100 on one line
#to the file.
outfile.write(str(num))
#Close the file.
outfile.close()
print('Data written to numbersmake.txt')
#Call the main function
main()
Ive done quite abit of research but I just cant figure out what I am missing. Help?
Upvotes: 1
Views: 10081
Reputation: 1
It only write one number or a line each time and there an error at line #Produce the numbers for count in range(12): #Get a random number. num = random.randint(1, 100)
not work i repair this by removw for count and put randomlist.randint(1, 36), 12)
because me want roulette number make 0 to 36 so 37 number addition all result = 666
The devil game
Not able to put more then one data in then file me wahnt they use the file to predict number after enter a lot of game of casino somebody can help me iT's a good start event there an error inside:
#This program writes 1 line of 0-36 random integers, each in the #range from 0-36 that make 37 number if addition all toguetter arrive at 666 the Devil Games to a text file.
def main():
import random
#Open a file named winning_number.txt.
outfile = open('winning_numbber.txt', 'w')
print('666 Winning Roulette by DjSOPEM (Patrick Émile Mercier 2022')
#Produce the numbers
for count in range(7): #this not work
#Get a random number.
num = random.randint(0, 36)
#Write 7 random intergers in the range of 0-36 on one line this not work
#to the file.
outfile.write(str(num))
#Close the file.
outfile.close()
print('Data written to winning_number.txt')
print(num)
#Call the main function main()
Upvotes: 0
Reputation: 626
Your write statement needs to be inside your for loop:
for count in range(12):
#Get a random number.
num = random.randint(1, 100)
#Write 12 random intergers in the range of 1-100 on one line
#to the file.
outfile.write(str(num) + ' ')#adds a space, unless you want the numbers to be all togerther
Your Write statement should be:
outfile = open('numbersmake.txt', 'a+')
So it will not overwrite the text already written, and it will create a new 'numbersmake.txt' if it doesn't exist.
Upvotes: 1
Reputation: 1193
All you need to do is place your write()
statement inside of your for
loop.
for count in range(12):
#Get a random number.
num = random.randint(1, 100)
#Write 12 random intergers in the range of 1-100 on one line
#to the file.
outfile.write(str(num))
Upvotes: 6