有幾個輸出問題與我的嵌套循環,通常我用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年?或者是所有年份的總數以及所有年份的月平均值?正確的答案取決於知道你想要做什麼。不清楚。 –
基於用戶輸入多年的所有月份的總和平均值爲 –