jeudi 5 mars 2015

Writing a random number generator to a text file

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?





Aucun commentaire:

Enregistrer un commentaire