# read in list of numbers # # Matt Bishop, MHI 289I, Winter 2019 # import sys # the list of numbers; it's initially empty numlist = [] # # first, open the file # it must be in tthe same directory as the current # working directory in which this program runs # exit on error # try: fhandle = open("numbers.txt", "r") except Exception as msg: print("Can't open file:", msg) sys.exit(1) # # now loop through, reading a line at a time # for line in fhandle: # convert the line to a number; leading or trailing # blanks ignored, but blanks in the middle is an error try: x = int(line) except: print("That's not a number") else: # it;s a number; add the integer value to the list numlist.append(x) # # sanity check: print what was read as a list # print(numlist) # # now add them up! # sum = 0 for i in numlist: sum += i # # print out the sum # print("sum is", sum)