← Intro to programming with Python
Mini project: a three-question quiz
Ask questions, check answers and keep score.
A quiz uses almost everything so far: input to ask, string tools to tidy the answer, if to check it, and a variable to keep score. The score starts at 0 and goes up by 1 for each right answer. Tidying with .strip().lower() makes the quiz fair, so "Paris", " paris" and "PARIS" all count.
score = 0
answer = input("Capital of France? ")
if answer.strip().lower() == "paris":
print("Correct!")
score += 1
else:
print("Not quite, it is Paris.")
print(f"Score: {score}")The two lines under the if are both indented, so both run when the answer is right.
Common mistake: comparing a typed number to a number. input() gives "4", and "4" == 4 is False. Convert with int(), or compare to the string "4".