How to create Flashcard App in Python.
About the project:
This Python Flashcard App is a beginner-friendly project that helps you learn how to build an interactive command-line application. The app displays questions and answers as flashcards, making it useful for studying vocabulary, programming concepts, or exam topics.
Users can add their own flashcards, study them in random order, and track how many answers they get correct during each session. All flashcards are saved to a file so they remain available the next time the app runs.
Flashcard App Overview
This Python project demonstrates how to build a complete menu-driven application using core programming concepts.
It uses file handling and JSON storage to permanently save flashcards, randomization to shuffle questions, and user input validation to ensure smooth interaction.
How the Flashcard App Works
The Flashcard App follows a simple workflow:
- Flashcards are stored as question-answer pairs in a JSON file
- When the program starts, existing flashcards are loaded from the file
- The user interacts with a menu to add or study flashcards
- During study mode, flashcards are shuffled randomly
- Results are shown at the end of each study session
This approach introduces beginners to real-world Python application design.
Sample Program Output
--- Python Flashcard App --- Menu: 1. Add a new flashcard 2. Study flashcards 3. Quit and Save Enter your choice: 1 Enter the question for the new flashcard: What is Python? Enter the answer for the new flashcard: A programming language Flashcard added. Enter your choice: 2 Question: What is Python? (Press Enter to reveal the answer) Answer: A programming language Did you get it right? (yes/no): yes You answered 1 out of 1 correctly.
Project Level: Beginner
Concepts Covered: Functions, loops, file handling, JSON, random module, user input
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 stepsStep 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()
Conclusion
This Flashcard App is an excellent beginner Python project that combines logic, file storage, and user interaction. It demonstrates how real applications store data, process user input, and provide meaningful feedback.
You can extend this project by adding categories, timed quizzes, or difficulty levels to improve your learning experience.
If you are learning Python basics, you may also find these tutorials useful:
← Back to Projects
