Having troubles writing a program to deal with an empty file, I wrote an if statement to deal with it and tried other methods but they did not work, not sure what I'm missing.
import statistics file = None def main ( ): while True: try: file = input("Enter Name of input file: ") inputfile = open(file, "r") if(inputfile == " "): print("The file is empty") total = 0 count = 0 num_list = [] #Read and display the file's contents for line in inputfile: number = int(line) total += number count += 1 num_list.append(number) # append current number to the list
max_number = max(num_list) # get max value from the list of numbers min_number = min(num_list) # get min value from the list of numbers range_of_list = (max_number - min_number) median_of_list = statistics.median(num_list) mode_of_list = statistics.mode(num_list) # close the file inputfile.close() print("File Name: ",inputfile.name) print("Sum: ", total) print("Count: ", count) print("Average: ", total / count) print("Maximum: ", max_number) print("Minimum: ", min_number) print("Range: ", range_of_list) print("Median: ", median_of_list) print("Mode: ", mode_of_list) perform_again = input("Would you like to evaluate another file of numbers? (yes/no): ") if (perform_again == "yes"): continue else: break except: print('The input file is not found, please re-enter a file name.') main ( )
https://stackoverflow.com/questions/66728196/dealing-with-an-empty-file March 21, 2021 at 10:04AM
没有评论:
发表评论