Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Writing your own functions (5/5) | Functions
Learn Python from Scratch
course content

Conteúdo do Curso

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

bookWriting your own functions (5/5)

But what if in our examples we pass as country argument some inappropriate name or name which does not exist in our dictionary? Let's predict it with some conditional statements!

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

Tarefa

Modify your previous function (name new as people_information_mod) so now it will detect if the name is in the dictionary. If not, print "There is no information about name"

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 7. Capítulo 6
toggle bottom row

bookWriting your own functions (5/5)

But what if in our examples we pass as country argument some inappropriate name or name which does not exist in our dictionary? Let's predict it with some conditional statements!

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

Tarefa

Modify your previous function (name new as people_information_mod) so now it will detect if the name is in the dictionary. If not, print "There is no information about name"

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 7. Capítulo 6
toggle bottom row

bookWriting your own functions (5/5)

But what if in our examples we pass as country argument some inappropriate name or name which does not exist in our dictionary? Let's predict it with some conditional statements!

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

Tarefa

Modify your previous function (name new as people_information_mod) so now it will detect if the name is in the dictionary. If not, print "There is no information about name"

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

But what if in our examples we pass as country argument some inappropriate name or name which does not exist in our dictionary? Let's predict it with some conditional statements!

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

Tarefa

Modify your previous function (name new as people_information_mod) so now it will detect if the name is in the dictionary. If not, print "There is no information about name"

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 7. Capítulo 6
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt