2016-09-25 67 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)