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
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

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:

List of 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:

Oceanian countries

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!


def get_country_dict(Argument_or_arguments_to_go_here):

# this needs writing!


def print_results(Argument_or_arguments_to_go_here):

# this needs writing!


# 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


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):

List of Oceania countries

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?

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post