2016-09-27 47 views
0

我的问题是我的降雨用户输入总量不增加正确不能得到内部循环加起来正确

我的假设是,当多年问道

它不加入的第一个月

problem explained

def main(): 

    #define accumulators 
    monthRain = 0 
    year = 0 
    monthTotal = 0 
    months = 0 
    total = 0 

    #get # of years 
    year = int(input("Enter the number of years to collect data for: ")) 

    #define month total befor it is changed below with year + 1 
    monthTotal = year * 12 

    #define how many months per year 
    months = 12 

    #Find average rainfall per month 
    for year in range(year): 
     #accumulator for rain per month 
     total = 0 
     #get rainfall per month 
     print('\nNext you will enter 12 months of rainfall data for year', year + 1) 
     for month in range(months): 
      print("Enter the rainfall for month", month + 1, end=" ") 
      monthRain = float(input(': ')) 

      #add monthly raingfall to accumulator 
      total += monthRain 
      average = total/monthTotal 

    #total months of data 
    print('\nYou have entered data for', monthTotal,'months') 

    #total rainfall 
    print('\nThe total rainfall for the collected months is:', total) 
    print('The average monthly rainfall for the collected months is:', format(average, '.2f')) 

main() 

回答

2

采取total = 0出来的for循环。每次进入循环时,它都会将total设置为零。把它拿出来放在for循环之前。我认为这会解决它。

让我知道...

+0

我爱你......我会在4分钟内正确选择你的答案 –

+0

没问题。快乐编程 –

+0

这真的是我的大脑........它感觉很高兴知道这是一个小错误,造成这一切,一切工作完美:D –

1

在每年入境的开始,您零total,然后在最后打印total。你看到的64个实际上是第二年的降雨量,第一年被抛出。

+0

你是对的....但有人打你2分钟,但谢谢你! –

0

我认为你不应该重置每年的total。这就是为什么你错过了一年的数据价值。您提供的示例中的降雨总量为131,平均值大约为5.46。我运行你的例子,得到以下。

Enter the number of years to collect data for: 2 
('\nNext you will enter 12 months of rainfall data for year', 1) 
Enter the rainfall for month 1 
: 3 
Enter the rainfall for month 2 
: 4 
Enter the rainfall for month 3 
: 6 
Enter the rainfall for month 4 
: 7 
Enter the rainfall for month 5 
: 9 
Enter the rainfall for month 6 
: 8 
Enter the rainfall for month 7 
: 5 
Enter the rainfall for month 8 
: 3 
Enter the rainfall for month 9 
: 4 
Enter the rainfall for month 10 
: 1 
Enter the rainfall for month 11 
: 8 
Enter the rainfall for month 12 
: 9 
('\nNext you will enter 12 months of rainfall data for year', 2) 
Enter the rainfall for month 1 
: 3 
Enter the rainfall for month 2 
: 6 
Enter the rainfall for month 3 
: 7 
Enter the rainfall for month 4 
: 3 
Enter the rainfall for month 5 
: 4 
Enter the rainfall for month 6 
: 1 
Enter the rainfall for month 7 
: 9 
Enter the rainfall for month 8 
: 7 
Enter the rainfall for month 9 
: 8 
Enter the rainfall for month 10 
: 4 
Enter the rainfall for month 11 
: 5 
Enter the rainfall for month 12 
: 7 
('\nYou have entered data for', 24, 'months') 
('\nThe total rainfall for the collected months is:', 131.0) 
('The average monthly rainfall for the collected months is:', '5.46') 

以下是更正代码:

#define accumulators 
monthRain = 0 
year = 0 
monthTotal = 0 
months = 0 
total = 0 

#get # of years 
year = int(input("Enter the number of years to collect data for: ")) 

#define month total befor it is changed below with year + 1 
monthTotal = year * 12 

#define how many months per year 
months = 12 

#Find average rainfall per month 
for year in range(year): 
    #accumulator for rain per month 
    #get rainfall per month 
    print('\nNext you will enter 12 months of rainfall data for year', year + 1) 
    for month in range(months): 
     print ("Enter the rainfall for month", month + 1, " ") 
     monthRain = float(input(': ')) 

     #add monthly raingfall to accumulator 
     total += monthRain 
     average = total/monthTotal 

#total months of data 
print('\nYou have entered data for', monthTotal,'months') 

#total rainfall 
print('\nThe total rainfall for the collected months is:', total) 
print('The average monthly rainfall for the collected months is:', format(average, '.2f')) 
+0

你是对的....但有人打你2分钟,但谢谢你! –

0

64实际上是你第二年的结果。

因为您的循环内有total = 0,所以每年之后是休息。因此,第一年的价值被第二年的价值所覆盖。

total=0以外的循环。这应该解决问题。

除此之外,我想知道,为什么你有一个月的变量,如果你使用常数12来计算monthTotal

+0

你是对的....但有人打你2分钟,但谢谢! –

+0

没关系。很高兴我可以帮助你:) – JHolub