2017-03-27 28 views
0

我创建了一个程序,它将读取由价格列表组成的.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 
+0

应该在两周之间的变化是价值观的差异?你为什么分裂? –

+0

你是对的,我刚刚解决了这个问题。我现在正在获取正确的最小值和最大值。仍然没有得到正确的平均变化率。 –

+0

您对平均变化率有什么价值? –

回答

1

这段代码,我想,应该做你想要什么:

# 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]) 

    #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 

    print priceList 

    for j in range (startWeekVal-1, endWeekVal): 
     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() 

有几个问题,你迭代的范围增加了一个。这意味着如果你试图开始1周和结束2周,它实际上是2和3。然后,您试图跟踪每个子阵列的第三个条目中的更改百分比,然后再次对其进行平均。而是跟踪第三项的大小变化,然后在最后平均。

0

我会推荐这样的事情

def stats(y, start, stop): 
    maximum = max(y[start, stop]) 
    minimum = min(y[start, stop]) 
    average = sum(y[start, stop])/len(y[start, stop]) 
    return maximum, minimum, average 

with open('prices.txt') as f: 
    l = list(map(float, f.readlines())) 

x = l[0] 
y = [] 
for i in l: 
    y.append(i-x) 
    x=i 

#We now have two lists. l[i] has the price at week i, and 
    #y[i] has the change in price from week i-1 

start_week = int(input("Start Week: ")) -1 
end_week = int(input("End Week: ")) 

maximum, minimum, average = stats(y, start_week, end_week) 

print("Maximum {}".format(maximum)) 
print("Minimum {}".format(minimum)) 
print("Average {}".format(average)) 
相关问题