Question: This is my code and I pass the test 1. But I failed all the function test (function test:find min_percent;function test:find max_percent;function test:find_gdp). Please help




This is my code and I pass the test 1. But I failed all the function test (function test:find min_percent;function test:find max_percent;function test:find_gdp). Please help me solve this. (Python program)
GDP txt link: https://www.cse.msu.edu/~cse231/Online/Projects/Project03/GDP.txt
Video about this:https://www.youtube.com/watch?v=tOlzPag97JI&feature=youtu.be
def open_file(): ENOENT = True while ENOENT: try: filename = input("Enter a file name: ") fp = open("GDP.txt") if filename == "" else open(filename) return fp except IOError: print("Error. Please try again") def find_min_percent(line): '''Find the min percent change in the line; return the value and the index.''' #print(line) min_val=100000000000 x = 0 str_line = line[76:] while x
def find_max_percent(line): '''Find the max percent change in the line; return the value and the index.''' max_val=0 x = 0 str_line = line[76:] while x float(max_val): max_val = current_value index = (x*12) # print(min_val) str_line = str_line[12:] x+=1 return max_val, index+1
def find_gdp(line, index): '''Use the index fo find the gdp value in the line; return the value''' #print("index=",index) index = int(index)+74 str_line = line[index:] #print("str_line=",str_line) gdp_value = str_line[:12] gdp_value = float(gdp_value)/1000 return gdp_value
def display(min_val, min_year, min_val_gdp, max_val, max_year, max_val_gdp): '''Display values; convert billions to trillions first.''' print("Gross Domestic Product") print("min/max change year GDP (trillions)") print("{:8.1f}{:>6d}{:>18.2f}".format("min",float(min_val),int(min_year),float(min_val_gdp))) print("{:8.1f}{:>6d}{:>18.2f}".format("max",float(max_val),int(max_year),float(max_val_gdp)))
def main(): a_file = open_file() line_count = 0 for line in a_file: line = line.strip() #print(line) line_count +=1 if line_count == 8: year_line = line
if line_count == 9: min_value = find_min_percent(line) max_value = find_max_percent(line) str_min_value = str(min_value) find_year_index = str_min_value.find(",",0) min_value_index = str_min_value[int(find_year_index)+1:len(str_min_value)-1] min_value=str_min_value[1:find_year_index] #print("min_value=",min_value) #print("min_value_index=",min_value_index.rstrip()) #Get Year for Min Value min_year_index = int(min_value_index.strip())+74 str_line = year_line[min_year_index:] year_min = str_line[:8] #get max_value year str_max_value = str(max_value) find_year_index = str_max_value.find(",",0) max_value_index = str_max_value[int(find_year_index)+1:len(str_max_value)-1] max_value=str_max_value[1:find_year_index] #print("max_value=",max_value) #print("max_value_index=",max_value_index.rstrip()) #print("max_value=",max_value) #Get Year for Max value max_year_index = int(max_value_index.strip())+74 str_line = year_line[max_year_index:] year_max = str_line[:8]
if line_count == 44: gdp_min_value = find_gdp(line,min_value_index) gdp_max_value = find_gdp(line,max_value_index)
#Get Year for MinValue # min_year_index = int(min_value_index.strip)+74 # str_line = year_line[min_year_index:] # year_min = str_line[:8]
#print("min_value_index=",int(min_value_index.strip())) # year_min = find_year(year_line,int(min_value_index)).strip() #print("year_min=",year_min) # year_max = find_year(year_line,int(max_value_index)).strip() #print("year_max=",year_max) display(min_value,year_min,gdp_min_value,max_value,year_max,gdp_max_value) if __name__ == "__main__": main()
Assignment Specifications The lines of interest in the file GDP. txt are the 9h 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. Mirmir tests: I can provide any file with the same format as the provided GDP.txt-the number of rows will be the same (so you can select lines 9 and 44), but the number of columns may be different (i.e. different years) Assignment Notes Divide-and-conquer is an important problem solving technique. In fact, inside of every challenging problem is a simpler problem trying to get out. Your challenge is to find those simpler problems and divide the problem into smaller pieces that you can conquer. Functions can assist in this approach. We provide skeleton code on Mirmir that has all the function skeletons for you to fill in Hint: build the following functions one at a time and test each one before moving on to the next. 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 correct-simply 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
