Python write random integers to file on newline
Question: I need to make a program that generates 10 random integers between 10 and 90
and calls two separate functions to perform separate actions. The first one
(`playlist`) simply needs to print them all on one line without spaces, which
was easy. The second one (`savelist`) is giving me problems. I need to write
every number in the list `nums` to `angles.txt` with each number on a separate
line in order. No matter what I try I can't get them on separate lines and it
appears as one string on a single line. What am I missing?

    
    
    import random
    
    def main():
        nums = []
        # Creates empty list 'nums'
        for n in range(10):
            number = random.randint(10, 90)
            nums.append(number)
            # Adds 10 random integers to list
        playlist(nums)
        savelist(nums)
    
    def playlist(numb):
        index = 0
        while index < len(numb):
            print(numb[index], end=' ')
            index += 1
    
    def savelist(number):
        myfile = open('angles.txt', 'w')
        # Creates numbers.txt file
        number.sort()
        myfile.write(str(number) + '\n')
        myfile.close()
    
    main()
    


Answer: In savelist(), you need to loop through the list:

    
    
    myfile = open('angles.txt', 'w')
    # Creates numbers.txt file
    
    number.sort()
    
    for e in number:
      myfile.write(str(e))
    
    myfile.close()
    

When you send "nums" to savelist(), you are sending a list. If you just try to
write "numbers" to the file, it's going to write the whole list. So, by
looping through each element in the list, you can write each line to the file.

