I'm trying to print the pressure of each city.
Instead of using the cities in the list, it is returning the pressure of a city called 'city'.
Super new to this and can't find an answer to something this specific. thanks!!!!!!
owm = pyowm.OWM('eb68e3b0c908251771e67882d7a8ddff')
city = ["tokyo", "jakarta"]
for i in city:
city = owm.weather_at_place('city')
city = city.get_weather()
Use better names and things will become clearer:
import pyowm owm = pyowm.OWM('eb68e3b0c908251771e67882d7a8ddff') cities = ["tokyo", "jakarta"] for city in cities: weather = owm.weather_at_place(city).get_weather() print (weather.get_pressure()['press'])
Changes and rationales:
cities. A variable named
cityshould contain a city. Not a list of them.
citybecause that is what will be in the variable: the name of one city.
iis a poor name because you'd expect it to be an index, not the item, and Python
forloops don't work that way. See how nicely
for city in citiesexpresses what is actually happening?
weather_at_place()to use the variable
city(a city from the list) rather than the constant string
"city", as the latter would search for a city named City.
get_weather()rather than using
cityto hold the value returned by the former, because the intermediate value is never used. Also eliminates the use of the variable
cityfor something else that is not a city.
citybecause it contains the weather, not the city.
Readability is important, even (especially!) when the person reading the code is the one who wrote it. If you're getting confused now, writing the code for the first time, imagine how confused you'll be when you try to modify the code in six months!