【问题标题】:How to run the for-loop then make the calculations at the end如何运行for循环然后在最后进行计算
【发布时间】:2019-10-01 19:48:41
【问题描述】:

我正在解决一个问题,我必须根据用户输入 number of years 和用户输入 inches of each month 计算平均降雨量。

我想做的是:

计算每年的总计和最后的平均值,而不是在 for 循环的每次迭代中执行此操作,而我对如何执行此操作一无所知。

这是我的代码:

MONTHS = 12
total = 0.0

while True:
  try:
    years_of_rain = int(input('How many years of rainfall would you like to calculate?: '))

    if years_of_rain <= 0 :
        print('Invalid data. You must enter 1 or more years. Try again.')
        continue

    for y in range(years_of_rain) :
        jan = float(input('How many inches of rain fell in January of year ' + str(y + 1) + '?: '))
        feb = float(input('How many inches of rain fell in February of year ' + str(y + 1) + '?: '))
        mar = float(input('How many inches of rain fell in March of year ' + str(y + 1) + '?: '))
        apr = float(input('How many inches of rain fell in April of year ' + str(y + 1) + '?: '))
        may = float(input('How many inches of rain fell in May of year ' + str(y + 1) + '?: '))
        jun = float(input('How many inches of rain fell in June of year ' + str(y + 1) + '?: '))
        jul = float(input('How many inches of rain fell in July of year ' + str(y + 1) + '?: '))
        aug = float(input('How many inches of rain fell in August of year ' + str(y + 1) + '?: '))
        sep = float(input('How many inches of rain fell in September of year ' + str(y + 1) + '?: '))
        oct = float(input('How many inches of rain fell in October of year ' + str(y + 1) + '?: '))
        nov = float(input('How many inches of rain fell in November of year ' + str(y + 1) + '?: '))
        dec = float(input('How many inches of rain fell in December of year ' + str(y + 1) + '?: '))

        rain_average_calc = (jan + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec) / MONTHS
        total += jan + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec

        num_months = MONTHS * years_of_rain

        average = total / num_months

        print('The total amount of rain was ' + format(total , ',.2f') + ' inches' )
        print('the average amount of rain was ' + format(rain_average_calc , ',.1f') + ' inches per month.' )
        print(average)
        print(num_months)

    break
except:
    print('invalid. try again')

【问题讨论】:

    标签: python python-3.x loops for-loop


    【解决方案1】:

    在进入 for 循环之前声明total,这样它就不会在每次迭代时都被重置,每次迭代将y 年的总降雨量添加到total,然后在退出循环后计算并打印结果。像这样的:

    # Outside of for loop
    total = 0
    
    for y in range(years_of_rain):
         # Inside of for loop
    
         # Code for getting input for current year goes here
    
         total += jan + feb + mar + apr + may + jun + jul + aug + sep + oct + nov + dec
    
    # Outside of for loop, after it has finished
    
    num_months = MONTHS * years_of_rain
    average = total / num_months
    
    print('The total amount of rain was ' + format(total , ',.2f') + ' inches' )
    print('the average amount of rain was ' + format(average , ',.1f') + ' inches per month.' )
    

    【讨论】:

      【解决方案2】:

      尝试使用数据结构,在这种情况下,我建议创建一个列表来存储所有值并在 for 循环之前对其进行实例化。然后在for循环之后它仍然存在,您可以进行计算。

      【讨论】:

        猜你喜欢
        • 2019-03-06
        • 1970-01-01
        • 1970-01-01
        • 2013-05-14
        • 2018-11-14
        • 2017-06-20
        • 2015-02-11
        • 2021-06-12
        • 2011-06-27
        相关资源
        最近更新 更多