Writing generated numbers in text file in Python
Question: So I tried browsing other posts, and even asked a friend before resorting to
actually asking here. I have a homework assignment which calls for me to
create a program in Python, that generates random numbers based on how many
numbers a user inputs. For example, if they input that they want to generate 5
numbers... the program will do just that. Now my issue is that I created the
following:

    
    
    import random
    
    def main():
        howMany = 0
        numbers = 0
    
    
        howMany = int(input('How many numbers would you like to generate?: '))
    
        infile = open ('rand_write.txt', 'w')
    
        for n in range(1,howMany):
            numbers = random.randint(1,115)
        infile.write(str(numbers))
        infile.close()
    main()
    

Everything works alright, until it's time to actually get the 5 numbers onto a
text file. I can't... for the life of me.. figure out just what I'm doing
wrong. The program writes to the text file, but it only writes a random
number, not 5. I would greatly appreciate any pointers and guidance in
figuring out what I should do to solve this issue. Thank you very much!


Answer: Your indentation is wrong. You need to put tab in front of
`infile.write(str(numbers))` to make sure that it gets execute in each loop.
Otherwise you end up just writing the last number.

You may also want to write some separator between the numbers.

Finally you may want to make just one call to generate the random numbers as
follows:

    
    
    numpy.random.random_integers(1,115, howMany)
    

