Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Modifying Functions in Python | Functions in Python
Introduction to Python(ihor)

bookModifying Functions in Python

Reconsider the example with the country information. If the provided name parameter isn't found in the dataset, a missing key error will occur. However, this situation can be easily handled by implementing conditional statements.

1234567891011121314151617
# Data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942), 'Brazil': (8515767, 212559417), 'India': (3166391, 1380004385)} # Modify our function def country_information_mod(dict, name): if name not in dict.keys(): print("There is no information about", name) else: print("Country:", name) print("Area:", dict[name][0], 'sq km') print("Population:", round(dict[name][1]/1000000, 2), 'mln') # Testing the function country_information_mod(countries_dict, "USA") country_information_mod(countries_dict, "Ukraine")
copy

The dict.keys() method returns a view of all the dictionary keys. It's used here to check if the provided name exists among the keys. The message is more user-friendly and doesn't stop our program.

question mark

What happens when a key is not found in the dictionary in the provided function?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 9

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Still meg spørsmål om dette emnet

Oppsummer dette kapittelet

Vis eksempler fra virkeligheten

Awesome!

Completion rate improved to 1.67

bookModifying Functions in Python

Sveip for å vise menyen

Reconsider the example with the country information. If the provided name parameter isn't found in the dataset, a missing key error will occur. However, this situation can be easily handled by implementing conditional statements.

1234567891011121314151617
# Data countries_dict = {'USA': (9629091, 331002651), 'Canada': (9984670, 37742154), 'Germany': (357114, 83783942), 'Brazil': (8515767, 212559417), 'India': (3166391, 1380004385)} # Modify our function def country_information_mod(dict, name): if name not in dict.keys(): print("There is no information about", name) else: print("Country:", name) print("Area:", dict[name][0], 'sq km') print("Population:", round(dict[name][1]/1000000, 2), 'mln') # Testing the function country_information_mod(countries_dict, "USA") country_information_mod(countries_dict, "Ukraine")
copy

The dict.keys() method returns a view of all the dictionary keys. It's used here to check if the provided name exists among the keys. The message is more user-friendly and doesn't stop our program.

question mark

What happens when a key is not found in the dictionary in the provided function?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 9
some-alt