繁体   English   中英

制作了一个输入列表(我国家的地方)现在我正在尝试在列表中的地方输入温度

[英]Made a list of inputs (places in my country) now i am trying to input temperatur on the places in the list

所以我已经完成了第一个任务,使用输入制作一个地点列表

   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}"

接下来我想输入我在地点列表中输入的地点的温度。

所以现在我得到:

  • 你好。 写下并填写 7 个地方的列表:纽约
  • 你好。 写下并用 7 个地方填充列表:Ect ect ect

完成后,我希望文本更改为

  • 纽约的温度是多少?
  • 温度有什么影响

然后我希望它以一个包含地点和温度的新列表结束。

我试过了

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

只是为了开始,但我似乎不能将 append 与 int 一起使用。 所以当这个问题解决后,我需要找到一种方法来使循环 go 遍历列表中的所有位置。 然后打印出地点和温度

这样的事情应该有效。 几件事:

  • 你不需要有一个计数器变量,你可以只使用 places_list 的长度来打破第一个循环。
  • 你为这些地方做了正确的事情,只是对温度感到困惑。 为了将 append 和 object 添加到列表中,您需要执行以下操作:

list_name.append(element_name)

  • 我建议的解决方案基于使用 2 个不同的列表,并且您以正确的顺序附加位置和温度,以便它们对应。 另一种方法是使用字典(@ErnestBidouille 提供了一个基于此的答案)。
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}")


或者,您可以使用包含位置温度对的列表:

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]}")

例如,我使用城市温度字典以便能够迭代单个 object 并能够在以后轻松重用这些值。 除了 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')

减少解决方案:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM