Csci 1020 March 25, 2019 Program Assignment 10

Write a program which creates a file containing 10000 random numbers between 1 and 15 million, and write a program which reads such a file then prints out the sum of the numbers.

# p10.py
import random
f = open('num.txt', 'w')
for i in range(10000):
   n = random.randint(1,10)
   f.write( str(n) + "\n")
f.close()

# p10b.py
g = open('num.txt')
while True:
   n = g.readline()
   if n == '':
      break
   sum = sum + int(n)
print "The sum is", sum

Turn in 3 files:

  1. The program p10.py which creates a file of 10000 random numbers between 1 and 15 million.
  2. The file of 10000 random numbers created, say rand.txt
  3. The program p10b.py which opens a file of numbers, reads them all, then prints out the sum.

For a reminder, do not use spaces in your file names !

p10.py, numbers.txt, and p10b.py are excellent file names. You can use other file names if you wish, no huge restrictions, but not long files names and names with spaces. Many thanks on this note! If you wish to make comments, use comment lines with a leading "#" within your Python programs--and this should always be done. Don't use the filename itself to write lengthy descriptions!

Email these 3 files to me: ted@fdltcc.edu


Content is neither approved nor reviewed by FDLTCC.