Question: Python only please Data could be found that: http://www.cse.msu.edu/~cse231/Online/Projects/Project03/GDP.txt Please use the provided starter code: def open_file(): '''Repeatedly prompt until a valid file name allows
Python only please
Data could be found that:http://www.cse.msu.edu/~cse231/Online/Projects/Project03/GDP.txt
Please use the provided starter code:
def open_file(): '''Repeatedly prompt until a valid file name allows the file to be opened.''' pass # replace this line with your code
def find_min_percent(line): '''Find the min percent change in the line; return the value and the index.''' pass # replace this line with your code
def find_max_percent(line): '''Find the max percent change in the line; return the value and the index.''' pass # replace this line with your code
def find_gdp(line, index): '''Use the index fo find the gdp value in the line; return the value''' pass # replace this line with your code
def display(min_val, min_year, min_val_gdp, max_val, max_year, max_val_gdp): '''Display values; convert billions to trillions first.''' pass # replace this line with your code
def main(): pass # replace this line with your code
# Calls the 'main' function only when you execute within Spyder (or console) # Do not modify the next two lines. if __name__ == "__main__": main()
Assignment Specifications
The lines of interest in the file GDP.txt are the 9th line which has the annual change in GDP and the 44th line which has the value of GDP for each year. The data starts in column 76 and each of the 47 data items spans 12 columns (there are 47 years inclusively from 1969 through 2015). These numbers are important because you can use string slicing to extract them. For example, the first data item in a line can be extracted using the slice line[76:76+12].
Your task is to find the minimum and maximum change in GDP for the years 1969 through 2015 and to find the GDP value for those two years. See the sample output below. Your program will prompt for an input file and then display the output.
You must use specific functions as described below and you are not allowed to use collections such as lists, tuples and dictionaries.
Assignment Notes
1. The open_file()function takes no arguments and returns a file pointer. It repeatedly prompts for file names until a file successfully opens. Use the try-except construct checking for the FileNotFoundError exception. You do not need to check that the filename is correctsimply check that the file opened. The simplest version of this function has no error checking so the body of the function is simply fp = open(GDP.txt) return fp Hint: Start with that as your function body and add the try-except error checking later.
2. The find_min_percent(line) function takes one argument, one line (str) from the GDP.txt file. It returns the minimal value (float) in that line and an index (int) indicating where the minimal value is in the line (you get to decide what value is the indexthere are multiple possibilities). You can return two values simply by separating them by commas: return min_value, min_value_index Here is an algorithm to find a minimal value of a series of values that you read. The algorithm is written in pseudo-code so it looks somewhat like Python, but needs details of Python to be complete: min_value = 10000000 # some large value for each value if value < min_value: # you have found a smaller value min_value = value # set min_value to that smaller value
3. The find_max_percent(line) function takes one argument, one line from the GDP.txt file. It returns the maximum value in that line and an index indicating where the maximum value is in the line. This function is nearly identical to the find_min_percent function. Instead of starting with a large value you start with a small value and reverse the operator in the Boolean expression.
4. The find_gdp(line,index) function takes two arguments, one line from the GDP.txt file and an index into the line. It returns the GDP value in that line at the specified index. Note that this function can be used to find the GDP value associated with the minimal percent change as well as the maximal percent changesimply call the function with a different index.
5. The display(min_val, min_year, min_val_gdp, max_val, max_year, max_val_gdp) function takes six arguments: the minimal value, year and GDP, and the same three for maximum (value, year, GDP). The function does not return anything. It displays the values as shown in the sample below. Note that the GDP values in the file are in billions whereas we expect you to convert billions to trillions for the output. Use the following format string for displaying values: "{:<10s}{:>8.1f}{:>6d}{:>18.2f}"
6. The main() function loops through the file to access lines 9 and 44, and sets up values for the call to display function.
7. You are not allowed to use collections such as list, tuples and dictionaries. Specifically, do not use the string .split() methodit returns a list. Use string slicing to extract values from lines in the file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
