Python 平均温度

Python 平均温度,python,Python,我的程序有问题,因为它没有列出年份和温度。还有,我怎样才能打印一条信息,说明气温比前一年有所上升 这是我的密码: years = int(input('How many years do you want to calculate the temperature outside?')) first = int(input('What year would you want to start calculating?')) month = 12 for year in range(1, years

我的程序有问题,因为它没有列出年份和温度。还有,我怎样才能打印一条信息,说明气温比前一年有所上升

这是我的密码:

years = int(input('How many years do you want to calculate the temperature outside?'))
first = int(input('What year would you want to start calculating?'))
month = 12
for year in range(1, years, + 1):
    for months in range(1, 13):
     get = float(input('What is the average temperature for month {} '.format(months)))
     if get < -100 or get > 200:
      print('Repeat a valid number.')
     get += get + months
     get = get / 12
print('Years\tTemperature')
print('-------------------')
print(years, '\t', format(get, '.2f'), sep ='')
years=int(输入('您想计算室外温度多少年?'))
first=int(输入('您希望开始计算的年份?'))
月份=12
对于范围内的年份(1年,+1年):
在范围(1,13)内的月份:
get=float(输入({}月的平均温度是多少)。格式(月)))
如果get<-100或get>200:
打印('重复有效数字')
获取+=获取+个月
get=get/12
打印('年\温度')
打印('-------------')
打印(年,'\t',格式(获取,'.2f'),九月='')

如果您首先将所有用户输入存储在一个列表中,然后处理并打印随后生成的结果,那么这将相对容易

这样做时,您可以跳过检查前一年的平均值,先给它一个初始值
None
,然后在计算确定是否有增长时检查该值

我的意思是:

years = int(input('How many years do you want to calculate the temperature outside? '))
first = int(input('What year would you want to start calculating? '))
data = []

for year in range(first, first+years):
    print('\nEnter temperatures for year', year)
    month_temps = []
    for month in range(1, 13):
        prompt = "What's the average temperature for month {}? ".format(month)
        # Loop until user enters a valid monthly temperature.
        while True:
            try:
                avg_temp = float(input(prompt))
            except ValueError:
                print("Sorry, didn't understand that, try again.")
                continue
            if -100 <= avg_temp <= 200:  # Valid number?
                break
            else:
                print("Sorry, but that's not valid, try again.")
                continue
        month_temps.append(avg_temp)

    # Compute the year's average temperature from the monthly ones.
    avg_temp = sum(month_temps) / len(month_temps)
    data.append((year, avg_temp))

print()
fmt_spec = '{:<4} {:>22} {:>16}'
print(fmt_spec.format('Year', 'Temperature', 'Increase'))
prev_temp = None
for year, avg_temp in data:
    difference = (avg_temp - prev_temp) if prev_temp is not None else 0
    print(fmt_spec.format(year, avg_temp, 'Yes' if difference > 0 else ''))
    prev_temp = avg_temp
years=int(输入('您想计算室外温度多少年?'))
first=int(输入('您希望开始计算的年份?'))
数据=[]
对于范围内的年份(第一年、第一年+第二年):
打印(“\n输入年份的温度”,年份)
月份时间=[]
对于范围(1,13)内的月份:
prompt=“月{}的平均温度是多少?”.format(月)
#循环,直到用户输入有效的月温度。
尽管如此:
尝试:
平均温度=浮动(输入(提示))
除值错误外:
打印(“对不起,我不明白,请再试一次。”)
持续

如果-100我想你是想问两件事:1-如何列出年份,2-如何说上一年列出的温度。但是你在问题中没有说清楚。