#!/usr/bin/python # TYPE:lang # DESC:Something to help you with studying vocabulary. Uses flash-card-like techniques to help you learn by asking for the words, and remembers which words you got wrong to help later on. #MODULES from dircache import listdir from re import sub import random #CONSTANTS y, n = 1, 0 #FUNCTIONS def choose_list(): possible = listdir('.') lists = {} for i in range(len(possible)): if possible[i].split('.')[-1] != 'vlst': pass else: lists[len(lists)] = possible[i] print "Possible vocab list files:" for list in lists.keys(): header = open(lists[list]).readlines()[0] if header.split('%%')[0] == 'vocablist': print list, header.split('%%')[1].title().strip() else: lists.remove(list) while 1: print 'Which shall we work with?' chosenone = input('>>> ') if lists.has_key(chosenone): return lists[chosenone] def make_list(): print '1 for new list, 2 to add to list' todo = input('>>> ') if todo == 1: print 'Title of vocabulary list?' listname = raw_input('>>> ') listfilename = sub('[\s. ]', '', listname) + '.vlst' listfile = open(listfilename, 'w') listfile.write('vocablist%%'+listname+'\n') put_words(listfile) elif todo == 2: print 'Choose an existing vocab list' listfilename = choose_list() listfile = open(listfilename, 'a') put_words(listfile) def load_list(listfile): wordlist = open(listfile).readlines()[1:] wordarray = [] print "Do you want this english to language (1) or language to english (2) ?" if input(">>> ") == 2: e, l = 1, 0 else: e, l = 0, 1 for line in range(len(wordlist)): eng = wordlist[line].split(':')[e].strip() lng = wordlist[line].split(':')[l].strip() wordarray.append([eng,lng]) random.shuffle(wordarray) return dict(wordarray) def put_words(listfile): print 'Insert the desired words, in the format "english:anglais". Type "quit" when finished' keepgoing = 1 while keepgoing: words = raw_input('>>> ') if words == 'quit': keepgoing = 0 elif ':' in words: listfile.write(words.strip()+'\n') print "Cleaning up..." listfile.close() print "Done!" def take_test(words): wrongs = {} for q, a in words.items(): print q guess = raw_input('>>> ') if guess.lower().strip() == a: print 'Hooray!' else: print 'Correct answer was "%s"' % a wrongs[q] = a print if len(wrongs) > 0: print "You got the following words wrong:" for word in wrongs.keys(): print word print "Would you like to go over them? (y/n)" if input('>>> '): take_test(wrongs) #MAIN while 1: print ''' Welcome to the vocab tester! Please choose an option. 1: Get tested. 2: Make (or add to) list. 3: Quit. ''' try: todo = input('>>> ') except ValueError: pass if todo == 1: wordlist = choose_list() words = load_list(wordlist) take_test(words) elif todo == 2: make_list() elif todo == 3: break