如果输入值为年= 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
什么是语法错误,代码是什么?把它放在问题上。 –