🎯 MILESTONE 2 - Basic flashcard app

If you’ve managed to complete the exercises 9 to 16, you have built a command line flashcard application while learning about data structures and loops.

🔑 Core Concepts

Data Structures

  • Lists []: Ordered collections of items that you can modify
    • Example: words = ["Dog", "Cat", "House"]
    • Used for: Storing sequences of items in order
  • Dictionaries {key: value}: Store pairs of information (like a real dictionary)
    • Example: flashcards = {"Dog": "Hund", "Cat": "Katze"}
    • Used for: Looking up one piece of info based on another (question → answer)
  • Dictionary Methods:
    • .items() - Get both keys and values
    • .keys() - Get only the keys
    • .values() - Get only the values
    • len(dictionary) - Count how many pairs

For Loops

  • Iterating through collections: Do something for each item
    for item in my_list:
        print(item)
    
  • Iterating through dictionaries: Get both key and value
    for key, value in my_dictionary.items():
        print(f"{key}{value}")
    
  • Loop control: Use break to exit a loop early

🏛️ Programming Patterns

1. Collection Processing Pattern (For Loop)

# Process each item in a collection
for item in collection:
    # Do something with each item
    process(item)

Used in: Going through each flashcard E.g.

for english_word, german_word in flashcards.items():
    print(f"English: {english_word}")
    print(f"German: {german_word}")

2. Dictionary Lookup Pattern

dictionary = {"key1": "value1", "key2": "value2"}
result = dictionary[user_input]

Used in: Getting correct answers for questions (e.g. translations of words) E.g.

flashcards = {"Dog": "Hund", "Cat": "Katze"}
correct_answer = flashcards[english_word]

3. Counted Loop with Break Pattern

count = 0
for item in collection:
    if count >= limit:
        break
    count += 1
    # Process item

Used in: Practicing only N cards E.g.

cards_practiced = 0
for english_word, german_word in flashcards.items():
    if cards_practiced >= num_flashcards:
        break
    cards_practiced += 1
    # Practice this card

4. Accumulator Pattern (For Loop)

total = 0
for item in collection:
    if condition:
        total += 1

Used in: Counting correct answers E.g.

cards_correct = 0
for english_word, german_word in flashcards.items():
    user_answer = input("Your answer: ")
    if user_answer.lower() == german_word.lower():
        cards_correct += 1

5. Input-Process-Output with Collections Pattern

# Input: Get data from user
# Process: Loop through collection and compare
# Output: Display results and score

Used in: The entire flashcard practice session E.g.

# Input: User sets number of cards
num_flashcards = int(input("How many cards? "))

# Process: Loop and check answers
for english_word, german_word in flashcards.items():
    user_answer = input(f"Translate: {english_word}")
    if user_answer.lower() == german_word.lower():
        cards_correct += 1

# Output: Show final score
print(f"Score: {cards_correct}/{cards_practiced}")

📖 Important Terms

Collection: A container that holds multiple items (lists and dictionaries are both collections).

Iteration: Going through each item in a collection one by one.

Key-Value Pair: In a dictionary, the key is used to look up the value (like word → translation).

Loop Variable: The variable that holds the current item in a for loop (e.g., english_word in for english_word in words:).

Accumulator: A variable that builds up a value as you loop (like cards_correct += 1).