Home > Enterprise >  Made a list of inputs (places in my country) now i am trying to input temperatur on the places in th
Made a list of inputs (places in my country) now i am trying to input temperatur on the places in th

Time:09-30

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
  • Hi. Write and fill the list with 7 places: 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. 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. Then print out places and temperatures

CodePudding user response:

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. Instead of first while True you can use a for too.

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')

CodePudding user response:

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)

CodePudding user response:

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.
  • 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:

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. Another way would be to use a dict (@ErnestBidouille provided an answer based on that).
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]}")
  • Related