文件中的每一行都包含特定日期一加仑汽油的平均价格。每行的格式如下:
MM-DD-YYYY:Price
MM表示两位数字的月,DD表示两位数字的日,YYYY表示四位数字的年。价格是指定日期每加仑汽油的平均价格。
您将编写一个或多个程序来读取文件的内容并执行以下计算:
平均价格每年:计算每年的平均价格,在文件中每一年。(该文件的数据始于1993年4月,截止于2013年8月。使用1993年和2013年的现有数据
示例运行:
The average price in 1993 was $1.07
The average price in 1994 was $1.08
The average price in 1995 was $1.16
The average price in 1996 was $1.24
The average price in 1997 was $1.24
The average price in 1998 was $1.07
The average price in 1999 was $1.18
The average price in 2000 was $1.52
The average price in 2001 was $1.46
The average price in 2002 was $1.39
The average price in 2003 was $1.60
The average price in 2004 was $1.89
The average price in 2005 was $2.31
The average price in 2006 was $2.62
The average price in 2007 was $2.84
The average price in 2008 was $3.30
The average price in 2009 was $2.41
The average price in 2010 was $2.84
The average price in 2011 was $3.58
The average price in 2012 was $3.68
The average price in 2013 was $3.65
现在我有这个:
#Opening GasPrice.txt
infile = open('C:/Users/Eeshaan M/Downloads/GasPrices.txt/')
#Read file content using deadline
gasPrices = float(infile.readline())
count = 0
# Strips the newline character
for line in gasPrices:
count += 1
print("Line{}: {}".format(count, line.strip()))
return Price
#Find average of price per year
averagePricePerYear = sum(gasPrice)/len(gasPrice)
#Find average of price per month
averagePricePerMonth = sum(gasPrice)/12
#Find Highest
print('Highest is ', max(gasPrices))
#Find Lowest
print('Lowest is ', min(gasPrices))
#Find Lowest to Highest
sorted_gasPrice = sorted(gasPrices)
print(sorted_gasPrice)
#Find Highest to Lowest
desendingOrder=gasPrice.sort(reverse = True)
print(desendingOrder)
在"return Price"处出现语法错误
我做错了什么?
return
不能像你那样使用,return
语句只能出现在函数内部。在本例中,只需删除该行。