我创建了一个程序,它将读取由价格列表组成的.txt文件。该计划将创建一个2维列表来存储周数,价格和前一周的变化。用户将输入所需的开始周和结束周。尝试从二维列表中查找平均变化率(从.txt文件读取)时,结果不正确
如果用户要输入的'开始一周为“1”和“端周”为“6”: 的平均变化应该是“-0.30” 的最高改变应该是“4周”随着变化'2.80' 最低变化应该是'第5周'与变化'-4.93'
但是,我的数字出来完全错误。
这里是我的代码:
# get two dimensional list
priceList = []
#open the file
priceFile = open ('MicrosoftPrices.txt', 'r')
#create variable for start year
week = 1
price = float (priceFile.readline())
#special case for week 1
#initialize loop counter
i = 0
while price != '':
priceList.append ([0,0,0])
priceList [i][0] = week
priceList [i][1] = float (price)
if week == 1:
priceList [i][2] = 0
else:
priceList[i][2] = ((priceList[i][1] - priceList[i-1][1])/priceList[i-1][1])
#read the next line
price = priceFile.readline()
#add to the counter
i += 1
#go to next week
week = week + 1
#initialize start and end weeks
startWeek = 0
endWeek = 0
#define start week validation
def FirstValidation (startWeek):
startWeek = -1
while startWeek == -1:
startWeek = input ('What week would you like to start with?')
try:
if startWeek == '':
startWeek = 1
break
startWeek = int (startWeek)
except Exception:
print ('Year must be a valid integer between 1 and 52')
else:
if startWeek >= 1 and startWeek <= 52:
break
else:
startWeek = -1
print ('ERROR: Week must be a valid integer between 1 and 52! Please try again.')
return startWeek
#define end week validation
def LastValidation (endWeek):
endWeek = -1
while endWeek == -1:
endWeek = input ('What week would you like to end with?')
try:
if endWeek == '':
endWeek = 52
break
endWeek = int (endWeek)
except Exception:
print ('Year must be a valid integer between 1 and 52')
else:
if endWeek >= startWeek and endWeek <= 52:
break
else:
endWeek = -1
print ('ERROR: Week must be a valid integer between 1 and 52! Please try again.')
return endWeek
def main():
#call week validations
startWeekVal = FirstValidation ('Start Week')
endWeekVal = LastValidation ('End Week')
#initialize min and max
maxChange = 0
minChange = 100
maxIndex = 0
minIndex = 0
total = 0
count = 0
for j in range (startWeekVal, endWeekVal +1):
if priceList [j][2] > maxChange:
maxChange = priceList [j][2]
maxIndex = j
if priceList [j][2] < minChange:
minChange = priceList [j][2]
minIndex = j
#calc average
total += priceList [j][2]
count += 1
#compute average
average = total/count
print ('Start Week:', startWeekVal)
print ('End Week:', endWeekVal)
print ('The average change is ', average)
print ('The week with the highest change is week' , priceList [maxIndex][0], 'with $', format (maxChange, '.2f'))
print ('The week with the lowest change is week' , priceList [minIndex][0], 'with $', format (minChange, '.2f'))
#close the file
priceFile.close()
#call main
main()
仅供参考,这里是.txt文件:
52.33
50.99
52.29
55.09
50.16
50.50
51.82
51.30
52.03
53.07
53.49
54.21
55.57
54.42
55.65
51.78
49.87
50.39
51.08
50.62
52.32
51.79
51.48
50.13
49.83
51.16
52.30
53.70
56.57
56.68
57.96
57.94
57.62
58.03
57.67
56.21
57.25
57.43
57.60
57.80
57.42
59.66
59.87
58.71
59.02
60.35
60.53
59.25
61.97
62.30
63.24
62.14
应该在两周之间的变化是价值观的差异?你为什么分裂? –
你是对的,我刚刚解决了这个问题。我现在正在获取正确的最小值和最大值。仍然没有得到正确的平均变化率。 –
您对平均变化率有什么价值? –