简体   繁体   English

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

[英]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:所以现在我得到:

  • Hi.你好。 Write and fill the list with 7 places: New York写下并填写 7 个地方的列表:纽约
  • Hi.你好。 Write and fill the list with 7 places: Ect ect ect写下并用 7 个地方填充列表:Ect ect ect

when this is done i want the text to change to完成后,我希望文本更改为

  • What is the temperature in New York?纽约的温度是多少?
  • What is the temperature in ect ect温度有什么影响

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:几件事:

  • you don't need to have a counter variable, you can just use the length of the places_list to break the first loop.你不需要有一个计数器变量,你可以只使用 places_list 的长度来打破第一个循环。
  • you were doing the right thing for the places, just got confused with the temperatures.你为这些地方做了正确的事情,只是对温度感到困惑。 In order to append an object to a list, you need to do:为了将 append 和 object 添加到列表中,您需要执行以下操作:

list_name.append(element_name)

  • the solution I suggested is based on using 2 different lists, and you're appending places and temperatures in the correct order, so that they correspond.我建议的解决方案基于使用 2 个不同的列表,并且您以正确的顺序附加位置和温度,以便它们对应。 Another way would be to use a dict (@ErnestBidouille provided an answer based on that).另一种方法是使用字典(@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}")


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.

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