import json

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest


class AddLocationForm(BoxLayout):

    search_input = ObjectProperty()
    search_results = ObjectProperty()

    def search_location(self):
        search_template = ("http://api.openweathermap.org/data/2.5/" +
                "find?q={}&type=like")
        search_url = search_template.format(self.search_input.text)
        request = UrlRequest(search_url, self.found_location)

    def found_location(self, request, data):
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        cities = ["{} ({})".format(d['name'], d['sys']['country'])
                 for d in data['list']]
        self.search_results.item_strings = cities


class WeatherApp(App):
    pass


if __name__ == '__main__':
    WeatherApp().run()

import json

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest


class AddLocationForm(BoxLayout):

    search_input = ObjectProperty()
    search_results = ObjectProperty()

    def search_location(self):
        search_template = ("http://api.openweathermap.org/data/2.5/" +
                "find?q={}&type=like")
        search_url = search_template.format(self.search_input.text)
        request = UrlRequest(search_url, self.found_location)

    def found_location(self, request, data):
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        if 'list' in data:
            cities = ["{} ({})".format(d['name'], d['sys']['country'])
                     for d in data['list']]
        else:
            cities = []
        self.search_results.item_strings = cities


class WeatherApp(App):
    pass


if __name__ == '__main__':
    WeatherApp().run()

Stop crashing when search doesn't have any matches
Stop crashing when search doesn't have any matches

  import json
  
  from kivy.app import App
  from kivy.uix.boxlayout import BoxLayout
  from kivy.properties import ObjectProperty
  from kivy.network.urlrequest import UrlRequest
  
  
  class AddLocationForm(BoxLayout):
  
      search_input = ObjectProperty()
      search_results = ObjectProperty()
  
      def search_location(self):
          search_template = ("http://api.openweathermap.org/data/2.5/" +
                  "find?q={}&type=like")
          search_url = search_template.format(self.search_input.text)
          request = UrlRequest(search_url, self.found_location)
  
      def found_location(self, request, data):
          data = json.loads(data.decode()) if not isinstance(data, dict) else data
+         if 'list' in data:
-         cities = ["{} ({})".format(d['name'], d['sys']['country'])
+             cities = ["{} ({})".format(d['name'], d['sys']['country'])
-                  for d in data['list']]
+                      for d in data['list']]
+         else:
+             cities = []
          self.search_results.item_strings = cities
  
  
  class WeatherApp(App):
      pass
  
  
  if __name__ == '__main__':
      WeatherApp().run()
  
Stop crashing when search doesn't have any matches
## Code Before:
import json

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest


class AddLocationForm(BoxLayout):

    search_input = ObjectProperty()
    search_results = ObjectProperty()

    def search_location(self):
        search_template = ("http://api.openweathermap.org/data/2.5/" +
                "find?q={}&type=like")
        search_url = search_template.format(self.search_input.text)
        request = UrlRequest(search_url, self.found_location)

    def found_location(self, request, data):
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        cities = ["{} ({})".format(d['name'], d['sys']['country'])
                 for d in data['list']]
        self.search_results.item_strings = cities


class WeatherApp(App):
    pass


if __name__ == '__main__':
    WeatherApp().run()

## Instruction:
Stop crashing when search doesn't have any matches
## Code After:
import json

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.network.urlrequest import UrlRequest


class AddLocationForm(BoxLayout):

    search_input = ObjectProperty()
    search_results = ObjectProperty()

    def search_location(self):
        search_template = ("http://api.openweathermap.org/data/2.5/" +
                "find?q={}&type=like")
        search_url = search_template.format(self.search_input.text)
        request = UrlRequest(search_url, self.found_location)

    def found_location(self, request, data):
        data = json.loads(data.decode()) if not isinstance(data, dict) else data
        if 'list' in data:
            cities = ["{} ({})".format(d['name'], d['sys']['country'])
                     for d in data['list']]
        else:
            cities = []
        self.search_results.item_strings = cities


class WeatherApp(App):
    pass


if __name__ == '__main__':
    WeatherApp().run()
