我的程序旨在让用户输入整个月的温度。然后我创建了一些功能,通过键入请求的日期来查看当天输入的温度,让用户知道一天的特定温度。然后编写了一些函数来显示平均温度,最低温度和最高温度(通过从列表中查找这些值,用户输入温度)。
编程效果不好,它可以在不崩溃的情况下回答用户但是没有显示正确的结果。它会“跳过”以显示请求的日期,并在用户请求最高/最低温度时打印出整个列表。当我试图获得平均值时,它会与messange崩溃:
Traceback (most recent call last):
File "C:\Users\Linnea\Documents\Studier\HT 2014\Introduktion till programmering\Linnea_Andersson.py", line 58, in <module>
main ()
File "C:\Users\Linnea\Documents\Studier\HT 2014\Introduktion till programmering\Linnea_Andersson.py", line 9, in main
functions(temp_list)
File "C:\Users\Linnea\Documents\Studier\HT 2014\Introduktion till programmering\Linnea_Andersson.py", line 53, in functions
print("Average temperature was: " + str(sum(temp_list)/float(len(temp_list), str(round(total,2)))))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
这是我的代码:
def main ():
temp_list = create_temp_list()
search()
functions(temp_list)
def create_temp_list ():
NUM_DAYS = 31
temp_list = [];
temperatur = [0] * NUM_DAYS
index = 0
print("Hi! Type in a temperature for each day in december!: ")
while index < NUM_DAYS:
print(index + 1, "/12", ": ", sep="", end="")
temperatur[index] = float(input())
index +=1
temp_list.append(temperatur)
return temp_list
def search():
temp_list = [1-31]
index = int(input("Vänligen skriv in en dag då du vill se temperaturen för: "))
while (index >= 1 and index < len(temp_list)):
print("The temperature this day was : ",(temp_list([index - 1])))
else:
print ("Ok")
x = [1,2,3]
try:
x[10]
except IndexError:
print("What are you trying to pull?")
def functions(temp_list):
svar1 =(input("To see the highest value, print 'ja': "))
if svar1 == "ja":
print("The highest temperature was: ", str(max(temp_list)))
else:
print ("This date wasn't found! You are now going to the next function.")
svar2 = (input("Too see the lowest temperature, print 'ja': "))
if svar2 == "ja":
print("Lowest temperature: ", str(min(temp_list)))
else:
print("This date wasn't found! You are now going to the next function.")
nyfiken = (input("To get the average temperature, print 'ja': "))
if nyfiken == "ja":
print("Average temperature was: " + str(sum(temp_list)/float(len(temp_list), str(round(total,2)))))
else:
print("This date wasn't found! The program is now closing.")
#Funktionen skriver ut medelsnittsvärdet
main ()
有人能帮帮我吗?
答案 0 :(得分:0)
> TypeError: unsupported operand type(s) for +: 'int' and 'list'*`
您尝试在 int 和 list 之间进行求和。首先在列表上创建一个for循环,访问它们的每个元素,然后就可以进行求和。
if nyfiken == "ja":
print("Average temperature was: " + str(sum(temp_list)/float(len(temp_list),str(round(total,2)))))
问题就在这里,正如我所说的那样在 temp_list 上创建 for 循环并访问每个元素,然后进行求和。
你也将它们转换为 string ,我不认为你可以用字符串进行求和。您将获得如下输出:
>>> a=10
>>> b=20
>>> str(a)+str(b)
'1020'
>>>
此外,您可以按照以下方式对值进行求和:
sum1=0
timer=0
y=int(input("How much values do you want to sum?: "))
while True:
x=int(input("Entry values: "))
timer+=1
sum1+=x #sum1=0, we summing every value with eachother. 0 is ineffective value in summation.
if timer==y:
print ("Summation of your values: ",sum1)
break
答案 1 :(得分:0)
你有很多问题;我们将按顺序接近它们:
create_temp_list
应该返回一个float列表 - 但由于你使用temp_list
和temperatur
的方式以及Python处理对象赋值的方式,它实际上是返回浮点列表的引用列表,即temp_list[0] == temp_list[1] == temp_list[2] ... == temperatur
。相反,尝试
NUM_DAYS = 31
def get_float(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
pass
def create_temp_list():
print("Hi! Type in a temperature for each day in december!: ")
make_prompt = "{}/12: ".format
return [get_float(make_prompt(day)) for day in range(1, NUM_DAYS+1)]
您永远不会将temp_list
传递给search()
;你的程序看起来应该更像
def main():
temp_list = create_temp_list()
search(temp_list) # <= pass the data to search!
functions(temp_list)
def search(temp_list):
...
由于(2),temp_list
不在范围内(您无法在search
中看到该变量)。要解决此问题,您尝试制作temp_list = [1-31]
等测试数据。这不符合你的想法;它创建一个包含值-30
的单项列表。然后len(temp_list)
总是1
,而index >= 1 and index < len(temp_list)
始终是False
- 这很好,因为否则你会被困在无尽的while
循环中! (应该是if ... else
,而不是while ... else
)。
希望有所帮助。