所以我写了一段代码来检测一个城市的最低温度,这取决于输入是什么,我现在的问题是我从循环的每个过程中得到每个单独的结果。因此,我最终得到的不是一份包含天气和城市的最终打印声明
[(-3, 'Toronto')]
The coldest city is: Toronto -3
[(-5, 'Edmonton'), (-3, 'Toronto')]
The coldest city is: Edmonton -5
[(-5, 'Edmonton'), (-4, 'Calgary'), (-3, 'Toronto')]
The coldest city is: Edmonton -5
我的输入是多伦多,-3 埃德蒙顿 -5,卡尔加里 -4
这是我的代码
dic = {}
for i in range(5):
city = input("Enter city followed by temperature >")
if (city != "stop"):
info = city.split()
dic[info[0]] = int(info[1])
elif city == "stop":
break
print(dic)
alist=[]
for k,v in dic.items():
alist.append((v,k))
alist.sort()
print(alist)
temp = alist[0]
print("The coldest city is:",temp[1], temp[0])