In: Computer Science
PYTHON......
Working with lists, functions, and files Objective: Work with lists Work with lists in functions Work with files Assignment:
Part 1: Write a program to create a text file which contains a sequence of test scores. Ask for scores until the user enters an empty value. This will require you to have the scores until the user enters -1.
After the scores have been entered, ask the user to enter a file name. If the user doesn’t not enter a file name exit the program. If the user does enter a file name, write out the scores to the specified file. Then ask the user if they wish to create another data file. Continue on with this process until the user indicated they do not wish to create another data file or they do not enter a file name.
Part 2: Write a program that will ask the user for a data file. Use one of the data files created from part 1. Each line of the file must contain a single value. Fill a list with the values from the file.
Once the list has been filled with values, the program should display, with appropriate labels, the largest value from the list, the smallest value from the list, and the average of the list (with 2 places after the decimal point). Then ask the user for a lower limit and an upper limit. Display all the values from the list that fall within that range.
The code to find the largest, smallest, and average must be done in value-returning functions. Do not use any built-in functions to find these values, write loops to implement these functions. The code to print out a range of scores must be done a function. The arguments for that function MUST be a list, a lower limit and an upper limit. The “matching values” function can print out values. For the rest of the program, only the main function will ask questions or print output.
createdatafiles.py: (part - 1)
Code:
if __name__ == '__main__': while True: values = [] print("Enter Data values: ") # read data values upto -1 while True: val = int(input()) if val == -1: break values.append(val) print("Enter file name: ", end='') file_name = input() # read file name # if file name is not entered break if len(file_name) == 0: break # if extension is not mentioned in the file name if not file_name.endswith('.txt'): file_name += '.txt' # creates and opens file and write values # if file already exists it overrides file = open(file_name, 'w+') for val in values: file.write(str(val) + '\n') file.close() # if the user wants to enter new files print("Enter new data values?(y/n): ", end='') option = input() if option.lower() == 'n': break
Screenshots of code for indentation:
Sample output for part 1:
readdatafiles.py: (part - 2)
Code:
def largest(values): max_val = values[0] for val in values: if val > max_val: max_val = val return max_val def smallest(values): min_val = values[0] for val in values: if val < min_val: min_val = val return min_val def average(values): sum = 0 for val in values: sum += val return sum / len(values) # returns list of values between limits def matching(values, upper, lower): match = [] for val in values: if lower <= val <= upper: match.append(val) return match if __name__ == '__main__': while True: print("Enter file name to perform analysis: ", end='') file_name = input() # if extension if not mentioned if not file_name.endswith('.txt'): file_name += '.txt' # check if file exists try: file = open(file_name, 'r') except FileNotFoundError: print("File not found") continue # read content and convert them into list of ints content = file.readlines() values = [int(line.strip()) for line in content] file.close() print("Largest value: ", largest(values)) print("Smallest value: ", smallest(values)) print("Average value: {0:.2f}".format(average(values))) print("Enter upper limit: ", end='') upper = int(input()) print("Enter lower limit: ", end='') lower = int(input()) # if a = [1, 2, 3], print(*a) => 1 2 3 print("Matching values: ", *matching(values, upper, lower)) print("Analyze new file?(y/n): ", end='') option = input() if option.lower() == 'n': break
Screenshots of Code for indentation:
Sample output of part - 2:
Please rate the answer if you like it.