Home python how to remove the error?

how to remove the error?

Author

Date

Category

When the code output occurs

if city_friends ['city'] ['title'] == City:
KEYERROR: 'CITY'

How to add a condition to me so that if this error appears, it’s just nothing to do and move to the next element of the dictionary


Answer 1, Authority 100%

More option:

if 'city' in city_friends and 'title' in city_friends ['city'] and city_friends ['city'] ['title'] == City:

Answer 2

arr = [
 {'City': {'Name': 'London', 'Pop': 12}},
 {}
]
for el in arr:
  Try:
    Print (EL ['City'])
  Except:
    Pass
# Either
for el in arr:
  if 'city' in el:
    Print (EL ['City'])

Answer 3

as option:

city = "hello world"
city_friends = {'city': {'title': 'Hello World'}}
_city = city_friends.get ('City')
If _city:
  _title = _city.get ('Title')
ELSE: _TITLE = NONE
If _title and _title == city:
  PRINT ("YES")

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions