-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashcard.py
63 lines (52 loc) · 1.42 KB
/
flashcard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Basic flashcard reader
# Author: Kenneth Zhang
# https://www.github.com/kenfzhang
# Usage:
# $ python flashcard.py <csv file>
# csv file rows have the format:
# <front of card>, <back of card>
import csv
import random
import sys
import os
def load_flashcards(path, topic):
with open(path) as csvfile:
cards = []
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in spamreader:
if len(row) == 2 or row[2] in topic or len(topic) == 0:
cards.append(row)
return cards
# Limits columns taken by text
def format_text(s,n):
s_new = ""
i = 0
new_lined = False
lines = 1
while i < len(s):
if s[i] == ' ' and lines * n < i:
s_new += "\n "
lines += 1
else:
s_new += s[i]
i += 1
return s_new
def main():
cards = load_flashcards(sys.argv[1], sys.argv[2:len(sys.argv)] if len(sys.argv) >= 3 else [])
random.shuffle(cards)
state = 'p'
os.system('cls' if os.name == 'nt' else 'clear')
i = 0
while i < len(cards) and state != 'q':
print("\n\n QUESTION: "+format_text(cards[i][0],50)+"\n")
state = input('')
print(" |")
print(" v\n")
print("\n ANSWER: "+format_text(cards[i][1],50)+"\n")
if len(cards[i]) > 2:
print(" ["+cards[i][2]+"]\n")
state = input('\n>============ ('+str(i+1)+' / '+str(len(cards))+')')
i += 1
if __name__ == "__main__":
# execute only if run as a script
main()