LPTHW Exercise 41 - Flashcards.py

Exercise 41 calls for making old fashion flashcards to memorize 10 definitions. I wrote a short script to automate this process (before I realized Zed did the same thing on the next page!). It was a good learning experience as it took me a few hours to figure out how to get the questions to appear randomly without replacement, and I found it quite useful for memorizing the definitions.

Without further adieu, here is flashcards.py

import random

print(’\n’)

“#” create pairs of terms: definitions (key=value)

Obj_Or_Dict = {
‘class’: “Tell Python to make a new type of thing”,
‘object’: “Two meanings - the most basic type of thing, and any instance of some thing”,
‘instance’: “What you get when you tell python to create a class”,
‘def’: “How you define a funcition within a class”,
‘self’: “Inside the functions in a class, ‘?’ is a variable for the instance/object being accessed”,
‘inheritance’: “The concept that one class can inherit traits from another class”,
‘composition’: “The concept that a class can be composed of other classes as parts”,
‘attributes’: “A property classes have that are from composition and are usually variables”,
‘is-a’: “A phrase to say something inherits from another things”,
‘has-a’: “A phrase to say that something is composed of other things or has a trait”
}

def FlashCards():
Obj_Or_List = list(Obj_Or_Dict.items())

    while len(Obj_Or_List) > 0:
        answer, question = random.choice(Obj_Or_List)

        print("What is:", question, "?\n")
        user_answer = str(input(">>> ")).lower()

        if user_answer == answer:
            print("\nCorrect!\n")
        else:
            print("\nIncorrect, dummy. The answer is '{}'\n".format(answer))

        Obj_Or_List.pop(Obj_Or_List.index((answer, question)))

FlashCards()

2 Likes

Nice, well done!

Challenge: Can you separate data and functionality in such a way that your script reads the flash cards from a file? Then you’d have a generic flash card trainer that you could use to drill whatever you want.

Tip: Adapt to Python’s internal naming style (only lowercase_with_underscores for function and variable names, CamelCase for class names). And try to find expressive names for things: obj_or_list doesn’t tell much about anything, why not call it cards? — That will make your code easier to read for others.

1 Like

@florian thanks for the tips, and challenge accepted! Here’s my updated flashcards.py

import random
from sys import argv

script, cards_file = argv
# cards_file is a text file that stores terms and definitions, separated by a colon; one pair of term: definition per line
# example - class: Tell Python to make a new type of thing

print('\n')

flashcards_list = []

with open(cards_file) as f:
    # create pairs of [term: definition] and store in flashcards_list
    for line in f:
        pairs = line.strip().split(':')
        flashcards_list.append(pairs)

def flashcards():

    while len(flashcards_list) > 0:
        #randomly select a pair of term: definition from flashcards_list
        answer, question = random.choice(flashcards_list)

        print("What is:", question, "?\n")
        user_answer = str(input(">>> ")).lower()

        if user_answer == answer:
            print("\nCorrect!\n")
        else:
            print("\nIncorrect, dummy. The answer is '{}'\n".format(answer))

        flashcards_list.pop(flashcards_list.index([answer, question]))

flashcards()

Curious, where can I find the naming conventions for Python? Perhaps I haven’t read far enough through the book yet.

1 Like

Nice! :slight_smile:

See here.