我創建了一個程序,它將讀取由價格列表組成的.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
應該在兩週之間的變化是價值觀的差異?你爲什麼分裂? –
你是對的,我剛剛解決了這個問題。我現在正在獲取正確的最小值和最大值。仍然沒有得到正確的平均變化率。 –
您對平均變化率有什麼價值? –