***NEED SOLUTION IN PYTHON ***
1. Create a main function which only gets executed when the file is directly executed
Hint:
def main():
pass
if __name__ == '__main__':
main()
2. Your programs will use a file called scores.txt to store a
bunch of user-provided numbers.
These can be int or float but their values must be between 0 and
100.
Inside the main function, display the current list of numbers to the user and ask if they want to update the list.
If currently no numbers are stored, display a message.
If the user wants to update the number list, provide them with options to do so. (Similar to how the movie programs adds or deletes movies)
Make sure to handle any possible exceptions in this step. Exceptions might occur if the user enters bad inputs or file is missing or cannot be read etc.
3. After they are done with the list of numbers, your program will display the following information about the list of numbers:
1. mean (or average)
2. mode
3. median
4. range
Write 4 different functions called mean, mode, median and range to get perform this task. Each of these functions will take in a list of numbers and return a number back except the range function which will return a tuple containing two numbers (min and max).
You can do a google search to find out the meanings of the 4 terms.
Do not use python's built-in functions to perform these tasks.
Make sure to write documentation and to perform exception handling in all four of these functions.
Below is the screenshot of the code. Comments are given explaining it.
Below are 2 outputs of the code:
1.
2.
Below is the code to copy:
#CODE STARTS HERE=======================
def range_of(li): #Finds min and max mini=float(li[0]) maxii=float(li[0]) for s in li: if float(s)<mini: #checks for smallest number mini = s if float(s)>float(maxii): #checkd for biggest number maxii = s ran = (mini,maxii) #creates tuple return ran def median(li): #finds middle number in the list count =0 for _ in li: count+=1 if count % 2 == 0: #if length is even m1 = li[count // 2] m2 = li[count // 2 - 1] med = (m1+m2) / 2 else: #if length is odd med = li[count // 2] return med def mode(li): #finds most repeating number d = dict() #uses dictionary to store frequency for s in li: if s in d: #updates dict if number present d[s] += 1 else: #creates new key if number is not present d[s] =1 maxi = 0 mod = 0 for key,val in d.items(): #finding the greatest frequency if val>maxi: maxi=val mod = key return mod def mean(li): #Finds average of list count =0 sum_of=0 for s in li: count+=1 #finds the length sum_of+= float(s) #finds total sum return sum_of/count def main(): with open("scores.txt",'r+') as f_r: #to display the existing values print("Below are the values in scores.txt") count =0 for score in f_r: count+=1 print(score, end="") if count == 0: print(" *The file is empty* \n") f_r.close() inp = input("Do you want to add scores?[y/n] ") #asking user for input scores_list = [] scores_list_full = [] if inp.strip().lower()== "y": print("Enter the numbers below. Enter 'q' to quit.") while True: try: #To catch any exceptions during value input by user s = input() if s.strip().lower() == "q": break if isinstance(s, (int, float)) or float(s)<0 or float(s)>100: raise Exception scores_list.append(s) except: #Prints error message and continues for next input print("Invalid Input") continue try: #To catch any exceptions while writing to the file with open("scores.txt","a+") as f: for score in scores_list: f.write(str(score)+"\n") except: print("Error in writing to the file") with open("scores.txt", 'r+') as f_r: for score in f_r: scores_list_full.append(float(score.strip())) #To store all values in the list for processing scores_list_full.sort() if len(scores_list_full)==0: exit() #function calls for processing the list of numbers mea = mean(scores_list_full) mod = mode(scores_list_full) med = median(scores_list_full) ran = range_of(scores_list_full) print("Mean :",mea,"\nMode :",mod,"\nMedian :",med,"\nran :",ran) if __name__ == '__main__': main() #this is used to run only when the file is directly executed
#CODE ENDS HERE=========================
***NEED SOLUTION IN PYTHON *** 1. Create a main function which only gets executed when the...
Questions 1. How to create a comment in python? 2. The way to obtain user input from command line 3. List standard mathematical operators in python and explain them 4. List comparison operators in python 5. Explain while loop. Give example 6. Explain for loop. Give example 7. How to create infinite loop? And how to stop it? 8. Explain a built-in function ‘range’ for ‘for’ loop 9. Explain break statement 10. Explain continue statement 11. Explain pass statement 12....
Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...
I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...
python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...
Hi, I need some help finishing the last part of this Python 1 code. The last few functions are incomplete. Thank you. The instructions were: The program has three functions in it. I’ve written all of break_into_list_of_words()--DO NOT CHANGE THIS ONE. All it does is break the very long poem into a list of individual words. Some of what it's doing will not make much sense to you until we get to the strings chapter, and that's fine--that's part of...
Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...
Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...
Python GPA calculator: Assume the user has a bunch of courses they took, and need to calculate the overall GPA. We will need to get the grade and number of units for each course. It is not ideal to ask how many courses they took, as they have to manually count their courses. Programming is about automation and not manual work. We will create a textual menu that shows 3 choices. 1. Input class grade and number of units. 2....
In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...
In Python and in one file please. (Simple functions with an expressions) Create a function called load_inventory(filename). The filename argument in this case specifies the name of a file that contains all the inventory/product information for the store, including product names, descriptions, prices, and stock levels. This function should clear any information already in the product list (i.e., a fresh start) and then re-initialize the product list using the file specified by the filename argument. You can structure your file...