【问题标题】:Cant get inner loops to add up properly无法正确添加内部循环
【发布时间】:2016-09-27 23:41:04
【问题描述】:

我的问题是我的用户输入的降雨总量没有正确加起来

我的假设是,当被问到多年时,它不会将第一个月添加为

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

【问题讨论】:

    标签: python loops nested


    【解决方案1】:

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

    让我知道...

    【讨论】:

    • 没问题。快乐编程
    • 这真的让我绞尽脑汁........知道这是一个小错误导致了这个感觉真好,而且一切都很完美:D
    【解决方案2】:

    在每年输入的开头,您将total 归零,然后在末尾打印total。你看到的64其实是第2年的降雨量,第1年被扔掉了。

    【讨论】:

      【解决方案3】:

      我认为您不应该每年都重置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' ))
      

      【讨论】:

        【解决方案4】:

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

        因为您的循环中有total = 0,所以每年之后它都是rest。因此,第一年的值会被第二年的值覆盖。

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-26
          • 2013-07-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多