有几个输出问题与我的嵌套循环,通常我用break
一些行添加到我的代码或print()
有python的输出问题;循环总数和平均数
当我在我的代码中使用print()
我的输出看起来像我打字上总计新行在一起,这是不是我想要的
以下是我的当前输出和我需要一个空行的图片;
第二件事:
我的代码没有被正确计算的信息找到每月总平均降雨量。
代码如下
def main():
#define accumulators
monthRain = 0
year = 0
monthTotal = 0
months = 0
total = 0
#get # of years
year = int(input("Enter the number of years to collect data for: "))
#blank line
print()
#define month total befor it is changed below with year + 1
monthTotal = year * 12
#define how many months per year
months = 12
#Find average rainfall per month
for year in range(year):
#accumulator for rain per month
total = 0
#get rainfall per month
print('Next you will enter 12 months of rainfall data for year', year + 1)
for month in range(months):
print("Enter the rainfall for month", month + 1, end='')
monthRain = float(input(': '))
#add monthly raingfall to accumulator
total += monthRain
average = total/monthTotal
#total months of data
print('You have entered data for', monthTotal,'months')
#blank line
print()
#total rainfall
print('The total rainfall for the collected months is:', total)
print('The average monthly rainfall for the collected months is:', average)
main()
您正在循环使用数年和数月。你想每年的总计和月平均超过1年?或者是所有年份的总数以及所有年份的月平均值?正确的答案取决于知道你想要做什么。不清楚。 –
基于用户输入多年的所有月份的总和平均值为 –