所以我想创建一个包含用户输入的不同温度的列表,然后打印出最高温度。
tempList = []
for i in range(0, int(input("How many temps? "))):
temp = (float(input("Input a temp: ")))
tempList.append(temp)
print("Temps are: ", tempList, "and the max temp is:", max)
然而,我正试图用另一种解决方案通过循环列表来做到这一点:
tempList = []
for i in range(0, int(input("How many temps? "))):
temp = (float(input("Input a temp: ")))
tempList.append(temp)
print(max(tempList))
max = 0.00
for temp in range(0, len(tempList)):
if temp > max:
max = temp
print("Temps are: ", tempList, "and the max temp is:", max)
直到最后打印出"max"为止,它工作得很好。当我输入36.5或者类似的值时,结果是2。我做错了什么?
- 不要使用内置函数名作为变量名
- 您正在迭代列表的索引
- 有一个内置函数
max
,你可以使用
tempList = []
for i in range(0, int(input("How many temps? "))):
temp = float(input("Input a temp: "))
tempList.append(temp)
print("Temps are: ", tempList, "and the max temp is:", max(tempList))
temperatures= []
for i in range(0, int(input("Enter Number of Temperatures You Want to Input: "))):
temp = (float(input("Enter Temperature: ")))
temperatures.append(temp)
print(f"Temperatures You Entered {temperatures}nMaximum Temperature:",max(temperatures))