[英]How to print the maximum number of a stream of inputs, before reaching the first 0? [Python]
[英]Print Number and the Variable before it in Python
我有一些代码要求用户输入,然后将其添加到数组,在用户输入其输入后,它将选择最小的整数并显示它。
my_array = []
while numberoftries <= numberofcities:
print("Please enter city", numberoftries)
city = input("")
print("Please enter the temprature for city", numberoftries)
citytemp = input("")
my_array.append(city)
my_array.append(citytemp)
print(my_array) # For testing
print(min(my_array)) # For testing
numberoftries = numberoftries ++ 1
唯一的问题是我想同时显示都添加到数组中的city和citytemp变量。我需要它显示数组中最小的整数,即。 print(min(my_array))
及其前面的变量,因为该变量将是城市名称。
任何帮助或朝着正确方向的观点都将很好
我建议您附加元组my_array.append((citytemp, temp))
而不是分别添加city
和citytemp
。 它将像魅力一样工作:
>>> min([(2,'toto'), (0,'tutu'), (1,'tata')])
(0, 'tutu')
之所以可行,是因为元组按照第一个元素的字典顺序排序,因此温度取决于您的情况。
您也可以像这样检索信息:
min_temp, min_city = min(my_array)
print min_city, "has lowest temperature:", min_temp
还要注意,您需要注意不要混淆整数和字符串, '13'
不是13
,字符串比较会导致意外结果:
>>>'25'<'Paris'
True
>>>'25'<' Paris '
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.