Key Error: "city"
V Vishwanathan
viradha at hotmail.com
Sat Sep 9 01:58:36 EDT 2017
(1) Trying to convert concatenated string to .format method
(2) concatenated string >>
[#todo rewrite this line to use the format method rather than string concatenation
alert = "Today's forecast for " + city + ": The temperature will range from " + str(low_temperature) + " to " + str(high_temperature) + " " + temperature_unit + ". Conditions will be " + weather_conditions + "."]
(3) My code:
city = "Seoul"
high_temperature = 18
low_temperature = 9
temperature_unit = "degrees Celsius"
weather_conditions = "light rain"
alert = "Today's forecast for {city}: The temperature will range from{low_temperature} "" to ""{high_temperature}{temperature_unit}Conditions will be {weather_conditions}".format(city,low_temperature,high_temperature,temperature_unit,weather_conditions)
print(alert)
(4) output:
Traceback (most recent call last):
File "D:\python exercises\uda format1.py", line 6, in <module>
alert = "Today's forecast for {city}: The temperature will range from{low_temperature} "" to ""{high_temperature}{temperature_unit}Conditions will be {weather_conditions}".format(city,low_temperature,high_temperature,temperature_unit,weather_conditions)
KeyError: 'city'
(5) Tried Google but not much help.
So would appreciate any help.
Thanks,
Venkat
More information about the Python-list
mailing list