# Array1 Example # Read an array from a file, then # compute the sum of the array elements. # Initialize array. This must be done #outside of the code block # File.open, otherwise values goes away when # exiting the code block. values = [ ] # Open input file. f = open("values1.txt", "r") # Read and process file. line = f.readline( ) while line: value = float(line.strip( )) values.append(value) line = f.readline( ) # Close input file. f.close( ) # Loop over the array elements to compute the sum. # The variable sum contains the partial sum after adding # each value. sum = 0.0 for x in values: sum += x # Print the sum with two digits after the decimal point. print(f"Sum = {sum}")