无法正确加入内循环

时间:2016-09-27 23:41:04

标签: python loops nested

我的问题是我的用户输入总数,因为降雨量没有正确累加

我的假设是,当多年被问及

时,它并没有增加第一个月

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()

4 个答案:

答案 0 :(得分:2)

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

让我知道......

答案 1 :(得分:1)

在每年的参赛作品开始时,您将total归零,然后在结尾处打印total。你看到的64实际上是第二年的降雨量,第一年就被淘汰了。

答案 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' ))

答案 3 :(得分:0)

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

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

在循环之外取total=0。这应该可以解决问题。

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