2016-09-25 79 views
0

如果输入值为年= 2,我如何才能让我的程序在前12个月显示第1年,并在接下来的12个月显示第2年?嵌套循环计算输出不正确,但程序运行

另外我不知道我的计算出错的地方。根据我的期望输出,总降雨量输出应该是37,但我正在逐渐39.

Added picture of current output. Total should be at 37 not 39. I also need the second batch of 12 months to say year 2

#the following are the values for input: 
#year 1 month 1 THROUGH year 1 month 11 = 1 
#year 1 month 12 THROUGH year 2 month 12 = 2 

def main(): 
    #desired year = 2 
    years = int(input("Enter the number of years you want the rainfall calculator to determine: ")) 
    calcRainFall(years) 

def calcRainFall(yearsF): 
    months = 12 
    grandTotal = 0.0 

    for years_rain in range(yearsF): 
     total= 0.0 
     for month in range(months): 
      print('Enter the number of inches of rainfall for year 1 month', month + 1, end='') 
      rain = int(input(': ')) 
      total += rain 
     grandTotal += total 
    #This is not giving me the total I need. output should be 37. 
    #rainTotal = rain + grandTotal 
    #print("The total amount of inches of rainfall for 2 year(s), is", rainTotal) 
    print("The total amount of inches of rainfall for 2 year(s), is", grandTotal) 
main() 
+0

您没有使用'years_rain'任何东西:

这招可以如下图所示还可以在最后打印行使用。尝试打印它而不是“年1”。 –

+0

,不会让我由于语法错误。 – Alina

+0

什么是语法错误,代码是什么?把它放在问题上。 –

回答

2

我缩短了一下代码。希望这是一个完整的,正确的程序:

def main(): 
    years = int(input("Enter the number of years you want the rainfall calculator to determine: ")) 
    calcRainFall(years) 

def calcRainFall(yearsF): 
    months = 12 * yearsF # total number of months 
    grandTotal = 0.0  # inches of rain 

    for month in range(months): 
     # int(month/12): rounds down to the nearest integer. Add 1 to start from year 1, not year 0. 
     # month % 12: finds the remainder when divided by 12. Add 1 to start from month 1, not month 0. 
     print('Enter the number of inches of rainfall for year', int(month/12) + 1, 'month', month % 12 + 1, end='') 
     rain = int(input(': ')) 
     grandTotal += rain 
    print("The total amount of inches of rainfall for", yearsF, "year(s), is", grandTotal) 

main() 
+0

这确实有效,你能否进一步解释它是如何做到的,我对于它如何能够进入第二年有点遗憾。 – Alina

+0

在前12个月(月= 0..11),月/ 12 = 0点。 未来12个月(月= 12..23),月/ 12 = 1点。 使用int()忽略小数点后的东西。它基本上给出从0开始的年数。因此,我们+ 1。 – Ahmad

3

print语句之前,您不必再为rainTotal增雨值。这是因为grandTotal每年都会造成下雨。它已经两年加两次了。所以,你在做什么本质上是增加的雨最后的值的两倍(在本例中2) 让您的打印语句这一点,并删除rainTotal -

print("The total amount of inches of rainfall for 2 year(s), is", grandTotal) 
+0

这个技巧。您能否帮助我再次说出第二年的产出,而不是第一年? – Alina

+0

在打印声明中使用 'print('输入年份'str(years_rain)+'month',str(month + 1)的降雨英寸数) ' – Zeokav

+0

这使得年份开始于0 – Alina

0
rainTotal = rain + grandTotal 

正在执行以下操作:2 + 37因为你还有最后雨输入= 2,已经全部或grandTotal是= 37(每年总的输入),所以rainTotal =雨+不需要grandTotal

+0

我能够在最近的编辑中改变它。你能否确定如何在第二年做出以下12个月而不是所有产出是第一年? – Alina

+0

所以你需要:第1年= 12个月,计算总数和第2年= 12个月并计算总数? – sb0709

+0

因为这是我输入的值,所以我的输出结果将会显示2年。我该如何做到这一点,因此这12个月被标记为“第1年”,其他12年被标记为“第2年”? “ ”输入年份降雨量的英寸数{这是我需要在下个月的下半月有2的地区}第1个月“ – Alina

0

注意您的代码:

如前所述,rainTotal是不必要的。

你可以试试:

print 'Enter the number of inches of rainfall for year %d month %d' % (years_rain, month), end='') 

这将填补第一个%d为years_rain的价值,并与值的第二%d为每月的for循环运行。

print("The total amount of inches of rainfall for %d year(s) % yearsF, is", grandTotal)