簡體   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