[英]Made a list of inputs (places in my country) now i am trying to input temperatur on the places in the list
So i have manage the first task, to make a list of places using input所以我已经完成了第一个任务,使用输入制作一个地点列表
places = []
count = 0
max_places = 7
while True:
count += 1
new_places = input("Hi. Write and fill the list with 7 places: ")
places.append(new_places)
if count == max_places:
break
print(f"The 7 places are {places}"
Next i want to input temperatures on the places that i input in the place list.接下来我想输入我在地点列表中输入的地点的温度。
so now i get:所以现在我得到:
when this is done i want the text to change to完成后,我希望文本更改为
And then i want it to end with a new list with places and temperatures side by side.然后我希望它以一个包含地点和温度的新列表结束。
I tried我试过了
places = []
count = 0
max_places = 7
new_temp = 0
max_temp = 7
while True:
count += 1
new_places = input("Hi. Write and fill the list with 7 places: ")
places.append(new_places)
if count == max_places:
while True:
count += 1
temp = input(f"What is the temperature in {places[0]} ?" )
temp.append(int(new_temp))
if count == maks_temp:
break
Just for a start, but i cannot use append with int it seems.只是为了开始,但我似乎不能将 append 与 int 一起使用。 So when that problem is solved i need to find a way to make the loop go trough all the places that i have in the list.
所以当这个问题解决后,我需要找到一种方法来使循环 go 遍历列表中的所有位置。 Then print out places and temperatures
然后打印出地点和温度
Something like this should work.这样的事情应该有效。 Couple of things:
几件事:
list_name.append(element_name)
places_list = []
temperatures_list = []
max_places = 3
while len(places_list) < max_places:
# populate list of places, break when reach the limit
new_place = input("Hi. Write and fill the list with 7 places: ")
places_list.append(new_place)
print(f"The 7 places are {places}")
# now that you have your list of places, go through them again and populate a different list for temperatures
for place in places_list:
temp = input(f"What is the temperature in {place}?")
temperatures_list.append(temp)
# print the corresponding places and temperatures using zip to navigate through the 2 lists together
for place, temp in zip(places_list, temperatures_list):
print(f"The temperature in {place} is {temp}")
Alternatively, you can use a list containing the place-temperature couples:或者,您可以使用包含位置温度对的列表:
places_list = []
places_temperatures_list = []
max_places = 3
while len(places_list) < max_places:
# populate list of places, break when reach the limit
new_places = input("Hi. Write and fill the list with 7 places: ")
places_list.append(new_places)
print(f"The 7 places are {places_list}")
for place in places_list:
temp = input(f"What is the temperature in {place}?")
places_temperatures_list.append((place, temp))
for place_temp in places_temperatures_list:
print(f"The temperature in {place_temp[0]} is {place_temp[1]}")
I use a city-temperature dictionary in order to be able to iterate on a single object and to be able to easily reuse the values later for example.例如,我使用城市温度字典以便能够迭代单个 object 并能够在以后轻松重用这些值。 Instead of first
while True
you can use a for
too.除了 first
while True
你也可以使用for
。
max_places = 7
places = []
for _ in range(max_places):
new_place = input(
f'Hi. Write and fill the list with {max_places} places: ')
places.append(new_place)
matched_place_temp = dict()
for place in places:
temp = int(input(f'What is the temperature in {place} ?'))
matched_place_temp.update({place: temp})
print(*(f'The temperature in {place} is {temp}'
for place, temp in matched_place_temp.items()),
sep='\n')
Reduced solution:减少解决方案:
max_places = 7
places = [
input(f'Hi. Write and fill the list with {max_places} places: ')
for _ in range(max_places)
]
matched_place_temp = {
place: int(input(f'What is the temperature in {place} ?'))
for place in places
}
print(*(f'The temperature in {place} is {temp}'
for place, temp in matched_place_temp.items()),
sep='\n')
places, temps = [], []
count = 0
while count < 7:
count += 1
new_places = input("Hi. Write and fill the list with 7 places: ")
places.append(new_places)
new_temp = input("What is the temperature in "+str(new_places)+" ?")
temps.append(new_temp)
places_temps = [{"Place": t, "Temperature": s} for t, s in zip(places, temps)]
print(places_temps)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.