Do you know the capital city of a country?
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
whileIs 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.
Questions to think about with this program to check your understanding:
Identify a Boolean variable in the program.
correct is a Boolean variable, also known as a flag.
Line 17 could be written in three different ways:
while not correct:while correct == False: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, ==, !=)?
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:
Change the program so that:
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.
# 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.")