Python 向列表中添加整数?

Python 向列表中添加整数?,python,Python,我试图通过“monthtemp”输入请求用户提供几个月的温度值,然后将所有值添加到列表中,最后打印出全年的平均值 count = 1 loops = int(input("How many years?: ")) listofmonthtemperatures= list() while count < loops: for i in range(1,loops+1): count += 1 year = input("Which is the " + str(i) +

我试图通过“monthtemp”输入请求用户提供几个月的温度值,然后将所有值添加到列表中,最后打印出全年的平均值

count = 1

loops = int(input("How many years?: "))
listofmonthtemperatures= list()
while count < loops:
for i in range(1,loops+1):
    count += 1
    year = input("Which is the " + str(i) + ": year?: ")
    for j in range (1,13):
        monthtemp= int(input("Month " + str(j) + ": "))
        listofmonthtemperatures.append(monthtemp)



total= sum(listofmonthtemperatures)
averagetempforyear= total / 12
print("The average temperature for", year, "is", averagetempforyear)
count=1
循环=int(输入(“多少年:”)
ListOfMonthTemperations=list()
当计数<循环时:
对于范围内的i(1,循环+1):
计数+=1
年份=输入(“哪个是“+str(i)+”:年份?:”)
对于范围(1,13)内的j:
monthtemp=int(输入(“月”+str(j)+“:”)
ListOfMonthTemperations.append(monthtemp)
总计=总和(月温度列表)
年平均温度=总计/12
打印(“年的平均温度”,即年的平均温度)

但是我得到的消息是,年份没有定义,但是我没有将它定义为用户输入的任何内容吗?第二,这行得通吗?为什么不能简单地将值附加到listofmonthtemperatures中?

我复制/粘贴了您的代码,它工作得很好。也许这是一个输入错误,但您的for循环没有正确缩进。我正在运行3.4.3 btw

count = 1
loops = int(input("How many years?: "))
listofmonthtemperatures= list()
while count < loops:
    for i in range(1,loops+1):
        count += 1
        year = input("Which is the " + str(i) + ": year?: ")
        for j in range (1,13):
            monthtemp= int(input("Month " + str(j) + ": "))
            listofmonthtemperatures.append(monthtemp)

total= sum(listofmonthtemperatures)
averagetempforyear= total / 12
print("The average temperature for", year, "is", averagetempforyear)
count=1
循环=int(输入(“多少年:”)
ListOfMonthTemperations=list()
当计数<循环时:
对于范围内的i(1,循环+1):
计数+=1
年份=输入(“哪个是“+str(i)+”:年份?:”)
对于范围(1,13)内的j:
monthtemp=int(输入(“月”+str(j)+“:”)
ListOfMonthTemperations.append(monthtemp)
总计=总和(月温度列表)
年平均温度=总计/12
打印(“年的平均温度”,即年的平均温度)

上面是正确缩进的工作代码。

我复制/粘贴了您的代码,它工作得很好。也许这是一个输入错误,但您的for循环没有正确缩进。我正在运行3.4.3 btw

count = 1
loops = int(input("How many years?: "))
listofmonthtemperatures= list()
while count < loops:
    for i in range(1,loops+1):
        count += 1
        year = input("Which is the " + str(i) + ": year?: ")
        for j in range (1,13):
            monthtemp= int(input("Month " + str(j) + ": "))
            listofmonthtemperatures.append(monthtemp)

total= sum(listofmonthtemperatures)
averagetempforyear= total / 12
print("The average temperature for", year, "is", averagetempforyear)
count=1
循环=int(输入(“多少年:”)
ListOfMonthTemperations=list()
当计数<循环时:
对于范围内的i(1,循环+1):
计数+=1
年份=输入(“哪个是“+str(i)+”:年份?:”)
对于范围(1,13)内的j:
monthtemp=int(输入(“月”+str(j)+“:”)
ListOfMonthTemperations.append(monthtemp)
总计=总和(月温度列表)
年平均温度=总计/12
打印(“年的平均温度”,即年的平均温度)

上面是带有适当缩进的工作代码。

在下面代码的开头初始化年份变量
count=1 year=0
(年份是一个局部变量,可以在outher for循环的范围内访问)好的,现在如果我在“循环”中要求超过一年,它就可以工作了但是如果我写1,它只打印0的平均温度是0.00@C1SC0用列表替换monthtemp变量,并将month tempreature变量附加到它们之后,您将能够获得平均温度/年(也可以在代码末尾编写一个循环,从
ListofMonthTemperations
中获得正确的月份值),例如:
AvgTemp=sum(ListOfMonthTemperations[0])/12#第一年平均温度等
在下面的代码开头初始化年份变量
count=1 year=0
(年份是一个局部变量,可以在outher for循环的范围内访问)好的,现在如果我在“循环”中要求超过一年,它就可以工作了但是如果我写1,它只会打印0的平均温度为0.00?@C1SC0用列表替换monthtemp变量,并将month tempreature变量附加到其中,然后就可以得到每年的平均温度(也可以在代码末尾写一个循环,从
listofmonthtemperatures
中获得正确的月份值)例如:
AvgTemp=sum(listofmonthtemperatures[0])/12#第一年的平均温度等