Do you know the capital city of a country?

Capital city

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

The new commands used in this program and others that may be useful. Select them below to learn more:

while

Is used to repeat an indented section of code underneath the statement until the condition is met/True. E.g. while x == 0 means repeat the code if x is still zero.

Repeated sections of code are called iterations or loops.

Boolean variables can hold a value of True or False. They are also known as flags. They are commonly used with while statements to ensure code repeats until a situation is either true or false.

You should never use an if command to repeat program statements. For example, the program below is not acceptable because the user only has one chance to get the answer wrong.

city = input("What is the capital city of England? : ")
if city != "London":
    city = input("What is the capital city of England? : ")

This is a very common mistake that new programmers make. You must always use a while to stop code continuing because you don’t know how many times an incorrect input might be made.

This mistake is easy to spot because if you use an input twice with the same statement, you’ve taken the wrong approach.

Investigate

Questions to think about with this program to check your understanding:

Item question

Identify a Boolean variable in the program.

Reveal answer

correct is a Boolean variable, also known as a flag.

Structure question

Line 17 could be written in three different ways:

  1. while not correct:
  2. while correct == False:
  3. while correct != False:

Which of these statements will not work, and how would you change it so that it does work without changing the operator (not, ==, !=)?

Reveal answer

Statement c will not work. It needs to say while correct does not equal True. In other words, while correct is False. This is written as:

while correct != True:

Make

Change the program so that:

  1. The user also has to guess correctly the capital city of Spain, which is Madrid.

Typical inputs and outputs from the program would be:

What is the capital city of England? London
What is the capital city of France? Paris
What is the capital city of Spain? Madrid
The quiz is complete.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
# Capital city program

# -------------------------
# Subprograms
# -------------------------
# Check if the answer is correct
def check_answer(country, city):
    # Capital city of England
    if country == "England" and city == "London":
        return True
    # Capital city of France
    if country == "France" and city == "Paris":
        return True
    # Capital city of Spain
    if country == "Spain" and city == "Madrid":
        return True
    # Invalid answer
    else:
        return False
---

# Quiz
def capital_cities():
---
    correct = False
---
    # Ask for first capital city until correct answer given
    while not correct:
---
        city = input("What is the capital city of England? ")
---
        correct = check_answer("England", city)
---
    
    correct = False
---
    # Ask for second capital city until correct answer given
    while not correct:
---
        city = input("What is the capital city of France? ")
---
        correct = check_answer("France", city)
---
    
    correct = False
---
    # Ask for third capital city until correct answer given
    while not correct:
---
        city = input("What is the capital city of Spain? ")
---
        correct = check_answer("Spain", city)
---


# -------------------------
# Main program
# -------------------------
---
capital_cities()
---
print("The quiz is complete.")