如果輸入值爲年= 2,我如何才能讓我的程序在前12個月顯示第1年,並在接下來的12個月顯示第2年?嵌套循環計算輸出不正確,但程序運行
另外我不知道我的計算出錯的地方。根據我的期望輸出,總降雨量輸出應該是37,但我正在逐漸39.
#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()
您沒有使用'years_rain'任何東西:
這招可以如下圖所示還可以在最後打印行使用。嘗試打印它而不是「年1」。 –
,不會讓我由於語法錯誤。 – Alina
什麼是語法錯誤,代碼是什麼?把它放在問題上。 –