(IN PYTHON) Download file homeruns.txt from Canvas. Your program must ask the user for the file name the read that file. Your program must catch an exception if the file isn’t found. It is a comma-delimited file with 2 fields on each line, a year and a number of home runs. Your program should have functions to determine the year with the most home runs, the year with the fewest number of home runs, and the average home runs for all years. You can have more functions if you desire. You should print the following information: the year with the most home runs and how many that is, the year with the fewest number of home runs and how many that is, and the average number of home runs per year. (IN PYTHON)
the following is what was in the file homeruns.txt
2011, 4552
2012, 4934
2013, 4661
2014, 4186
2015, 4909
2016, 5610
2017, 6105
2018, 5885
2019, 6776
Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.
CODE
#function to find most homeruns and year
def max_home_runs(year, homerun):
#initializing max, max_year, i
max = homerun[0]
max_year = year[0]
i = 0
#loop to find most homerun
for run in homerun:
#finding most homerun and
year
if run > max:
max = run
max_year =
year[i]
i = i + 1
#returns most homerun and year
return max, max_year
#function to find fewest homeruns and year
def min_home_runs(year, homerun):
#initializing min, min_year, i
min = homerun[0]
min_year = year[0]
i = 0
#loop to find fewest homerun
for run in homerun:
#finding fewest homerun and
year
if run < min:
min = run
min_year =
year[i]
i = i + 1
#returns fewest homerun and year
return min, min_year
#function to find average homerun
def average(homerun):
#initializing total
total = 0
#loop to find total homerun
for i in homerun:
#calculating total homerun
total = total + i
#calculating average homerun
avg = total / len(homerun)
#returns average homerun
return avg
#prompt for filename
filename = input("Enter your file name: ")
#Trying to open file
try:
file = open(filename, "r")
#catching exception if file not found
except:
print("File Not Found!")
#list to store year and homerun
year = []
homerun = []
#reading each line
for line in file:
#removing "\n" from each line
line = line.strip()
#spliting each line
data = line.split(",")
#appending year to list
year.append(data[0])
#appending homerun to list
homerun.append(int(data[1]))
#calling max_home_runs
max, max_year = max_home_runs(year, homerun)
#printing most homerun and year
print("Most home runs\t:", max, "Year:", max_year)
#calling min_home_runs
min, min_year = min_home_runs(year, homerun)
#printing fewest homerun and year
print("Fewest home runs:", min, "Year:", min_year)
#calling average
avg = average(homerun)
#printing average
print("Average:", "{0:.2f}".format(avg))
OUTPUT
CODE SCREEN SHOT
(IN PYTHON) Download file homeruns.txt from Canvas. Your program must ask the user for the file...
Starting out with python 4th edition Write program that lets the user enter in a file name (numbers.txt) to read, keeps a running total of how many numbers are in the file, calculates and prints the average of all the numbers in the file. This must use a while loop that ends when end of file is reached. This program should include FileNotFoundError and ValueError exception handling. Sample output: Enter file name: numbers.txt There were 20 numbers in the file....
Python Code Write a program using functions and mainline logic which prompts the user to enter a number. The number must be at least 5 and at most 20. (In other words, between 5 and 20, inclusive.) The program then generates that number of random integers and stores them in a list. The random integers should range from 0 to 100. (You can use a wider range if you want, but the lower end of the range must be at...
Python. Please attach python file if possible. All Instructions given: General The program must allow a trader to enter in stock's symbol, its current price, and a lucky number (see the validation rules for these inputs below). The program then outputs a recommendation to either buy, sell, or hold the stock. The recommendation is based on whether the current stock price is above, below, or equal to a particular threshold value specific to that stock (see more on the threshold...
Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...
Write a program that will accept from the user a text file containing hurricane data along with an output filename. The data consists of the year, the number of storms, the number of hurricanes, and the damage in millions of US Dollars. The first line contains the word “Years” followed by the first year listed and the last year listed separated by tabs. The following lines contain the data separated by tabs. A sample text file is available in this...
Problem 3 Rewrite Program 2. This is you must use keyword arguments to pass number of kWh and customer type to the bill_calculator function when it is called. Save your Python program in a file named Lab07P3.py. Submit the file to Blackboard for credit.This is what i wrote for prgram 2:(): kwh = (()) cus = (()) cus.upper() bill_calculator(kwhcus) (kwhcus): kwh <= cus == : price = kwh * kwh > cus == : price = * + ((kwh - ) * ) kwh <= cus == : price = kwh * kwh > cus == : price = * + ((kwh - ) * ) (.format(price))
Download the file patternMatch.c from the course website. This file contains part of a program to do pattern matching. The function main looks like this: int main() { char text (40), pattern (40), *position; int textlength, patternlength; printf ("Enter text: "); textlength = readline (text, 40); printf ("Enter pattern: "); patternlength = readline (pattern, 40); position = findmatch (pattern, text, patternlength, textlength); printmessage (position, text, patternlength, textlength); The function readline should read a line of characters from the keyboard. The...
Description: Create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the name of file, sum of numbers, count of numbers, average of numbers, maximum value, minimum value, and range of values. Purpose: The purpose of this challenge is to provide experience working with numerical data in a file and generating summary information. Requirements: Create a program called numstat.py that reads a series of integer numbers from a file and determines...
The code is in python. The file does not have to be the data set from HW2. What I need is a code combing selection sort and merge sort. The 2-1 is what the problem in refers to as Q1. d) [10 points] Write a code combining both selection sort and mergesort algorithms (NOT insertion and merge sorts) as described in Q1) to find out the most optimal k minimizing time efficiency of your algorithms using 1 M data set...