EXERCISE TOPIC▼
- Access exercises (91)
- C# exercises (79)
- Excel exercises (278)
- Power Apps exercises (13)
- Power Automate exercises (18)
- Power BI exercises (139)
- Power Platform exercises (157)
- Python exercises (28)
- Report Builder exercises (141)
- SQL exercises (198)
- SSAS exercises (51)
- SSIS exercises (46)
- SSRS exercises (99)
- VBA exercises (85)
- Visual Basic exercises (46)
PYTHON EXERCISES▼
PYTHON EXERCISES▼
Python | Functions exercise | Write a modular system to read and print out a list of countries
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
Software ==> | Python (28 exercises) |
Version ==> | Latest version |
Topic ==> | Functions (3 exercises) |
Level ==> | Harder than average |
Subject ==> | Python training |
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
The folder above contains 6 CSV files and one Python program file. Here are the CSV files:

Each of these is a text file containing a list of countries in that continent, together with the country's capital city.
Here's what the Oceania file looks like, for example:

The countries and capitals of Oceania.
The Python program contains this code, but it needs a bit of work!
def get_contents_of_file(Argument_or_arguments_to_go_here):
# this needs writing!
pass
def get_country_dict(Argument_or_arguments_to_go_here):
# this needs writing!
pass
def print_results(Argument_or_arguments_to_go_here):
# this needs writing!
pass
# choose continent to show data for
continent_name = "Oceania"
# get text from file for given continent name
country_list = get_contents_of_file(continent_name)
# from this extract a list of countries and cities
countries = get_country_dict(country_list)
# list them out
print_results(continent_name,countries)
For each function you should make sure you have the right number of arguments, give your arguments sensible names and add data type suggestions.
Your task is to write the functions so that when you run the program you get this (if you set the continent name to Oceania, for example):

What you should see for Oceania, for example.
When you've got this working, test if it works for a different continent name.
A final thought: what happens if you try the continent name as Antarctica? Can you make your program do something sensible in this case?