Can you name all of the 54 countries of Africa?
In this challenge we will write a Python script to create a quiz where we ask the user to guess as many African countries as possible. The player will score one point per correct country.
Using a list in Python
Our Python program will use a list to store all 54 countries.
countries = [“Algeria”, “Angola”, “Benin”, “Botswana”, “Burkina Faso”, “Burundi”, “Cabo Verde”, “Cameroon”, “Central African Republic”, “Chad”, “Comoros”, “Ivory Coast”, “Djibouti”, “Democratic Republic of the Congo”, “Egypt”, “Equatorial Guinea”, “Eritrea”, “Eswatini”, “Ethiopia”, “Gabon”, “Gambia”, “Ghana”, “Guinea”, “Guinea-Bissau”, “Kenya”, “Lesotho”, “Liberia”, “Libya”, “Madagascar”, “Malawi”, “Mali”, “Mauritania”, “Mauritius”, “Morocco”, “Mozambique”, “Namibia”, “Niger”, “Nigeria”, “Republic of the Congo”, “Rwanda”, “Sao Tome & Principe”, “Senegal”, “Seychelles”, “Sierra Leone”, “Somalia”, “South Africa”, “South Sudan”, “Sudan”, “Tanzania”, “Togo”, “Tunisia”, “Uganda”, “Zambia”, “Zimbabwe”]
To check how many items (countries) are in the list, we can use the len() function:
numberOfCountries = len(countries) print("There are " + str(numberOfCountries) + " countries in Africa!")
To check if a country is in the list we can use the keyword in as follows:
if "Kenya" in countries: print("Kenya is in Africa!")
To remove a country from the list, we can use the remove() function. For instance is the user has named a country which is in the list, we can remove this country from the list.
if "Kenya" in countries: countries.remove("Kenya")
Flowchart
Below is the full flowchart for our Python quiz: (Click on flowchart to zoom in)
Python Code
We have started the code, you task is to complete the code in the trinket window below:
Extension Task
Amend this code to give the player three lives. For each invalid answer, the player should lose a life. The game should stop and display a game over message when the player has found all 54 countries or when they have lost their 3 lives.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area