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()