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:
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: email@example.com