Motivation for dictionaries
To see why dictionaries are useful, have a look at the two lists defined in the script. countries contains the names of some European countries. capitals lists the corresponding names of their capital.
Diese Übung ist Teil des Kurses
Intermediate Python
Anleitung zur Übung
- Use the
index()method oncountriesto find the index of'germany'. Store this index asind_ger. - Use
ind_gerto access the capital of Germany from thecapitalslist. Print it out.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Definition of countries and capital
countries = ['spain', 'france', 'germany', 'norway']
capitals = ['madrid', 'paris', 'berlin', 'oslo']
# Get index of 'germany': ind_ger
# Use ind_ger to print out capital of Germany