Question: Python program. Having problems on the correct spacing. I keep getting syntax errors # readFile function implementation def readFile(infile): arr = [] #for line in
Python program. Having problems on the correct spacing. I keep getting syntax errors
# readFile function implementation def readFile(infile): arr = [] #for line in infile: # num = int(line) # arr.append(num) arr = infile.read().split(" ") arr[-1] = arr[-1].strip(' ') arr = list(map(int, arr)) return arr # getSum function implementation def getSum(arr): total = 0 for n in arr: total = total + n return total # getMaximum function implementation def getMaximum(arr): maximum = arr[0] for n in arr: if n > maximum: maximum = n return maximum # getMinimum function implementation def getMinimum(arr): minimum = arr[0] for n in arr: if n < minimum: minimum = n return minimum # getMedian function implementation def getMedian(arr): n = len(arr) for i in range(n-1): minPos = i for j in range(i + 1, n-1): if arr[j] < arr[minPos]: minPos = j if i != minPos: temp = arr[i] arr[i] = arr[minPos] arr[minPos] = temp if n < 1: return None if n % 2 == 1: return arr[n//2] else: return sum(arr[n//2-1:n//2+1])/2.0 # getMode function implementation def getMode(arr): _mode = [] numCounts = {} max_count = 0 for number in arr: if number not in numCounts: numCounts[number] = 0 numCounts[number] += 1 if numCounts[number] > max_count: max_count = numCounts[number] for number, count in numCounts.items(): if count == max_count: _mode.append(number) return _mode # main function implementation def main(): choice = "y" while choice == 'y' or choice == 'Y': filename = input('Enter the name of the file: ') try: infile = open(filename, "r") arr = readFile(infile) total = getSum(arr) count = len(arr) if count > 0: average = total / count else: average = 0 maximum = getMaximum(arr) minimum = getMinimum(arr) range1 = maximum - minimum median = getMedian(arr) mode = getMode(arr) print(' File name: ',filename) print('Sum: ',total) print('Count: ',count) print('Average: ',average) print('Maximum: ',maximum) print('Minimum: ', minimum) print('Range: ',range1) print('Median: ',median) print('Mode: ',mode) infile.close() except: print('The input file is not found!') choice = input(' Would you like to evaluate another file? (y/n): ') # call the main function main()
Can not figure out the right indents for program to work.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
