Flashcard App Project (code) in Python

← Back to Projects

Flashcard App in Python.

About the project: This app will present you with a series of flashcards containing a question on one side and an answer on the other.

It will keep track of your progress and allow you to study different topics.

You can easily add, remove, and edit the flashcards to create your own study sets.

Flashcard App:

This Python script provides a complete and interactive Flashcard App.

It has a menu-driven interface, allowing you to add new flashcards and then study them in a randomized order.

Your flashcards are saved to a JSON file, so they will be available the next time you run the app.


Project Level: Beginner

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: Follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# flashcard_app.py

import json
import os
import random

# Filename for storing flashcards
FLASHCARDS_FILE = "flashcards.json"

def load_flashcards():
    """
    Loads flashcards from a JSON file. If the file doesn't exist, it returns an empty list.
    """
    if os.path.exists(FLASHCARDS_FILE) and os.stat(FLASHCARDS_FILE).st_size > 0:
        try:
            with open(FLASHCARDS_FILE, 'r') as f:
                return json.load(f)
        except json.JSONDecodeError:
            print("Warning: Flashcard file is corrupted or empty. Starting with a new set.")
            return []
    return []

def save_flashcards(flashcards):
    """
    Saves the list of flashcards to a JSON file.
    """
    with open(FLASHCARDS_FILE, 'w') as f:
        json.dump(flashcards, f, indent=4)
    print("Flashcards saved successfully.")

def add_flashcard(flashcards):
    """
    Adds a new flashcard to the list.
    """
    question = input("Enter the question for the new flashcard: ").strip()
    answer = input("Enter the answer for the new flashcard: ").strip()
    
    if question and answer:
        flashcards.append({"question": question, "answer": answer})
        print("Flashcard added.")
    else:
        print("Question and answer cannot be empty. Flashcard not added.")

def study_flashcards(flashcards):
    """
    Allows the user to study the flashcards.
    """
    if not flashcards:
        print("No flashcards available to study. Please add some first.")
        return

    random.shuffle(flashcards)
    
    score = 0
    total_cards = len(flashcards)
    
    for card in flashcards:
        input(f"\nQuestion: {card['question']}\n(Press Enter to reveal the answer)")
        print(f"Answer: {card['answer']}")
        
        correct = input("Did you get it right? (yes/no): ").strip().lower()
        if correct == 'yes':
            score += 1

    print("\n--- Study Session Complete ---")
    print(f"You answered {score} out of {total_cards} correctly.")
    print("-----------------------------")

def main():
    """
    Main function to run the Flashcard App with a menu.
    """
    flashcards = load_flashcards()
    
    print("--- Python Flashcard App ---")
    
    while True:
        print("\nMenu:")
        print("1. Add a new flashcard")
        print("2. Study flashcards")
        print("3. Quit and Save")
        
        choice = input("Enter your choice (1, 2, or 3): ").strip()
        
        if choice == '1':
            add_flashcard(flashcards)
        elif choice == '2':
            study_flashcards(flashcards)
        elif choice == '3':
            save_flashcards(flashcards)
            print("Exiting Flashcard App. Goodbye!")
            break
        else:
            print("Invalid choice. Please enter 1, 2, or 3.")

# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
    main()