Four ways to write to a file

Printing to a file instead of to the conosole

The "print()" statement accepts an optional parameter that sends its output to a filehandle.

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 23 11:23:37 2021

@author: rmontant
"""
import random

def main(argv=[__name__]):
    if len(argv) == 3:
        filename = argv[1]
        n_values = int(argv[2])
    else:
        filename = input('filename? ')
        n_values = int(input('How many values? '))

    try:
        handle = open(filename, 'w')
    except Exception as e:
        print('Cannot open {:s} for writing!'.format(filename))
        print(e)
        return 1

    for n in range(n_values):
        r = random.random() * 200 - 100
        print('{:f}'.format(r), file=handle)

    handle.close()
#--------

if __name__ == '__main__':
    import sys
    sys.exit( main(sys.argv) )
#--------

Using a ".write()" statement

Build a string containing all the outputs, separated by newlines. Then write that string to the file. More efficient than using a series of ".write()" statements of shorter strings.

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 23 11:23:37 2021

@author: rmontant
"""
import random

def main(argv=[__name__]):
    if len(argv) == 3:
        filename = argv[1]
        n_values = int(argv[2])
    else:
        filename = input('filename? ')
        n_values = int(input('How many values? '))

    try:
        handle = open(filename, 'w')
    except Exception as e:
        print('Cannot open {:s} for writing!'.format(filename))
        print(e)
        return 1

    lines = []
    for n in range(n_values):
        r = random.random() * 200 - 100
        rstr = '{:f}'.format(r)
        lines.append(rstr)
    lines.append('')

    handle.write( '\n'.join(lines) )
    handle.close()
#--------

if __name__ == '__main__':
    import sys
    sys.exit( main(sys.argv) )
#--------

All output on one line

Build a string containing all the outputs on a single line, then write that string to the file. Just use the ".join()" list method as in the previous program, but join using a whitespace character instead of a newline.

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 23 11:23:37 2021

@author: rmontant
"""
import random

def main(argv=[__name__]):
    if len(argv) == 3:
        filename = argv[1]
        n_values = int(argv[2])
    else:
        filename = input('filename? ')
        n_values = int(input('How many values? '))

    try:
        handle = open(filename, 'w')
    except Exception as e:
        print('Cannot open {:s} for writing!'.format(filename))
        print(e)
        return 1

    lines = []
    for n in range(n_values):
        r = random.random() * 200 - 100
        rstr = '{:f}'.format(r)
        lines.append(rstr)
    lines.append('\n')

    handle.write( '  '.join(lines) )
    handle.close()
#--------

if __name__ == '__main__':
    import sys
    sys.exit( main(sys.argv) )
#--------

All output on one line, using "print()" statements

Print each value as it's created, use optional "print()" arguments to suppress newlines and send output to a file.

# -*- coding: utf-8 -*-
"""
Created on Fri Apr 23 11:23:37 2021

@author: rmontant
"""
import random

def main(argv=[__name__]):
    if len(argv) == 3:
        filename = argv[1]
        n_values = int(argv[2])
    else:
        filename = input('filename? ')
        n_values = int(input('How many values? '))

    try:
        handle = open(filename, 'w')
    except Exception as e:
        print('Cannot open {:s} for writing!'.format(filename))
        print(e)
        return 1

    for n in range(n_values):
        r = random.random() * 200 - 100
        print('{:f}'.format(r), end=' ', file=handle)

    print(file=handle)
    handle.close()
#--------

if __name__ == '__main__':
    import sys
    sys.exit( main(sys.argv) )
#--------