What you will build

A flashcard quiz is a useful practice project because it combines data, input, repetition, and feedback in one short program. This version asks a series of Python questions in the terminal. After each response, it shows whether the answer matches and displays the expected answer. At the end, it prints a score. The cards are shuffled so they need not appear in the same order each time.

You need Python 3.10 or later and a terminal. The program uses only Python’s standard library, so there are no packages to install. Create a file named flashcards.py in a folder you can find again. You will run it with python flashcards.py, or with python3 flashcards.py if that is how Python is launched on your system.

Store the cards and run the quiz

A list of pairs keeps each question next to its answer. The program makes a copy of that list before shuffling it, then visits each pair once. Enter the following plain-text code in flashcards.py:

import random cards = [ ("Which keyword starts a function definition in Python?", "def"), ("Which keyword begins a conditional branch?", "if"), ("Which built-in function displays output?", "print"), ("Which keyword starts a loop over items?", "for"), ] questions = cards.copy() random.shuffle(questions) score = 0 print("Python flashcard quiz") print("Type one-word answers, then press Enter.\n") for number, (question, answer) in enumerate(questions, start=1): print(f"Card {number} of {len(questions)}") response = input(f"{question} ").strip().casefold() if response == answer.casefold(): score += 1 print("Correct!") else: print(f"Not quite. Expected answer: {answer}") print() print(f"Final score: {score} out of {len(questions)}")

Save the file and run it from the terminal in the same folder. For example, type def when asked which keyword starts a function definition. You should see “Correct!” for that card. Type an unrelated word and the program should show the expected answer instead. Because the questions are shuffled, test the prompts themselves rather than expecting a particular first card.

Understand how the answer check works

The response passes through strip() to remove spaces at its beginning and end, then casefold() to make letter-case differences less likely to affect a match. As a result, “ DEF ” counts as def. The stored answer also passes through casefold() before comparison. This is deliberately a simple exact-answer quiz: it does not decide whether a longer explanation is conceptually correct.

The loop uses enumerate() to provide a card number while it reads each question-and-answer pair. A correct match increases score by one. Once the loop has visited every card, len(questions) provides the total for the final message. Copying cards before calling random.shuffle() leaves the original list in its written order, which can be helpful if you later want to reuse it elsewhere in the program.

Try entering a blank response to see what happens. It is treated as incorrect, and the expected answer appears. This is acceptable for a small quiz, but it is a design choice: a different flashcard app might use Enter to reveal an answer without counting the card as wrong.

Customize and test your own cards

Replace the sample pairs with facts you want to practice, keeping each question and answer inside its own pair. Prefer prompts with one short, unambiguous answer. For example, “Which Python keyword begins a conditional branch?” works better for exact matching than “Explain conditional logic.” If several answers would be reasonable, either rewrite the question or change the program to accept multiple answers.

After editing the list, run the quiz again. Check that a correct answer earns a point, an incorrect answer reveals the expected term, and the final total equals the number of cards. Also test answers with extra surrounding spaces and different capitalization. If you remove every card, the program will display a score of zero out of zero; add at least one card before using it as a study quiz.

For a next challenge, add a second round containing only missed cards. You could collect incorrect pairs in a new list during the first loop, then ask those questions again. Keep the first-round score separate so the final result remains clear. That extension turns a short exercise in lists and loops into a tool you can adapt to new subjects.

Back to category ↗