Question: Hi, I am trying to create a program that will read a file gas prices and print the highest and lowest price value. I did
Hi, I am trying to create a program that will read a file "gas prices" and print the highest and lowest price value. I did the following code, but for some reason it doesn't work. Can you please help me with that?
The format on the file is as follows: 04-05-1993:1.068 from 1993 to 2013
STARTING_YEAR = 1993 ENDING_YEAR = 2013
gas_file = open('gas prices.txt', 'r') gas_list = gas_file.readlines()
def get_price(str): items = str.split(':') return float(items[1])
def get_month(str): items = str.split('-') return int(items[0])
def get_day(str): items = str.split('-') return int(items[1])
def get_year(str): items = str.split(':') date_items = items[0].split('-') return int(date_items[2])
def display_highest_per_year(gas_list): current_year = get_year(gas_list[0]) highest = get_price(gas_list[0])
for e in gas_list: if get_year(e) == current_year: if get_price(e) > highest: highest = get_price(e) else: print ('highest price for ', current_year,': $',format (highest, '.2f'), sep = '') current_year = get_year(gas_list[0]) highest = get_price(gas_list[0])
print ('The highest price for', current_year, 'is', format(highest, '.2f'),sep='')
def display_lowest_per_year(gas_list): current_year = get_year(gas_list[0]) lowest = get_price(gas_list[0])
for e in gas_list: if get_year(e) == current_year: if get_price(e) < lowest: lowest = get_price(e) else: print ('lowest price for ', current_year,': $',format (lowest, '.2f'), sep = '') current_year = get_year(gas_list[0]) lowest = get_price(gas_list[0])
print ('The lowest price for', current_year, 'is', format(lowest, '.2f'),sep='')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
