This is part of my poker math game as there are topics from he book that work well for the format of flashcards. This is really simple program that demonstrates how to do several interesting things in python.
enter_message = 'Press enter to continue.'
__ = input(enter_message)
__ = input('')
help_message = 'This is a simple flashcards system that helps you remember important things from the book. The data for this app is in flashcards.csv. To show the answer press enter.'
with open('resources/flashcards.csv') as cards:
for card in cards:
# list compression to get rid of leading spaces after the comma
card = [s.strip() for s in card.split(',')]
front = card
back = card
if __name__ == '__main__':
The first couple functions show a simple way to wait for the user to input some data. The reason I’m assigning the output to __ is to show that I’m not going to use that data. The double underscore variable is a standard way of expressing that in python.
At the beginning of the main (line 16) I call clear to clear out the terminal window and give me a fresh looking space. This shows the simple way to call system commands from python.
On line 20 we use the
with ... as syntax to open the file. This is a special kind of statement for resources like files that need to be cleaned up when you finish using them. The advantage of
with is that when execution goes out of scope it will clean up the file which will close it. That means that if any of the code inside throws an exception the file will be closed.
Line 21 uses the pythonic line reading syntax to read from the file. The variable
cards refers to the file and
for line in file will loop through a file a line at a time in python.
Line 24 is the next interesting syntax piece. To understand it we need to break it down. The first thing that split is separating the line into a list of strings, splitting anytime it sees a comma. The second thing is the list compression. List compression is a syntax that efficiently does something on each item in a list and the syntax is
[action for item in list]. strip() is a function that removes whitespace and the beginning and end of a line and I’m using it specifically to clean up any extra whitespace in the csv file lines.
So there you have it, a simple flashcards python app you can use for studying. To follow the advancements in my python game go checkout my github repo and let me know what you think.