Capital city

Capital city

Do you know the capital city of a country?

Try


Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

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

Knowledge Organiser

Command summary

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:

a. while not correct:

b. while correct == False:

c. 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


Success Criteria

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.

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your code to check that your program has met the success criteria and produces the required output.

What is the capital city of England? : London

What is the capital city of France? : Paris

What is the capital city of Spain? : Rome

What is the capital city of Spain? : Berlin

What is the capital city of Spain? : Madrid

The quiz is complete.

Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist