Simple Python program to help improve your Spanish vocabulary
I’ve been taking 4 hours of Spanish lessons in Antigua, Guatemala for the last 4 days now. I’ve learned a lot in this last 4 days but I still have a ton to learn. My Spanish vocabulary needs a lot of work, so I wrote this simple Python program to help me.
The program loads a list of words from a CSV file (I export my CSV files from Google Docs), shuffles them, iterates through them, gives you the English word, and asks you to enter the Spanish word. If you answer correctly, it will go to the next word. If you answer incorrectly, you have the option to try again or tell the program to give you the answer.
The program keeps track of the words you were not able to answer and shows them to you once it finishes going through all the words for you to review.
import sys import csv import random def get_wordlist(filepath): wordlist = {} try: with open(filepath, 'rb') as f: reader = csv.reader(f, delimiter=',') for row in reader: wordlist['%s' % row[0].strip().lower()] = \ '%s' % row[1].strip().lower() except IOError: print "\nCould not open %s!\nExiting..." % filepath sys.exit(1) return wordlist def get_answer(english_word, wordlist): print "\n[%s word(s) remaining]" % len(wordlist.items()) print "\nEnglish: %s" % english_word return raw_input('Spanish: ').strip().lower() def get_option(): print "\n[1] Try again.\n[2] Show answer." return raw_input("\nEnter option number: ").strip().lower() if __name__ == '__main__': wordfile = raw_input('\nPlease enter the path of the word list file: ') wordlist = get_wordlist(wordfile.strip().lower()) # Shuffle the dictionary keys so the objects will be displayed # in random order to the user. keys = wordlist.keys() random.shuffle(keys) # Keep track of incorrect answers here. missed_words = {} # Pretty ugly for loop. Should refactor later (ok, I confess, # I probably won't get to it). for key in keys: def handle_correct_answer(): print "\nCorrecto!" wordlist.pop(key) def handle_incorrect_answer(): print "\nIncorrecto!" if get_answer(wordlist[key], wordlist) == key: handle_correct_answer() else: handle_incorrect_answer() while True: option = get_option() if option == '1': if get_answer(wordlist[key], wordlist) == key: handle_correct_answer() break handle_incorrect_answer() elif option == '2': print "\nThe correct answer is '%s'" % key.upper() missed_words[key] = wordlist[key] wordlist.pop(key) break else: print "\nInvalid option, amigo! Put down the cerveza." # Display words that were missed and the answers. if missed_words: print "\nBelow are the word(s) that you missed.\n" for key, value in missed_words.items(): print "%s - %s" % (key.upper(), value)
Sample Usage:
Please enter the path of the word list file: c:\verbs.csv [5 word(s) remaining] English: to be (temporary) Spanish: estar Correcto! [4 word(s) remaining] English: to like Spanish: gustar Correcto! [3 word(s) remaining] English: to exist (there is, there are) Spanish: existo Incorrecto! [1] Try again. [2] Show answer. Enter option number: 2 The correct answer is ‘HABER’ [2 word(s) remaining] English: to be (permanent) Spanish: ser Correcto! [1 word(s) remaining] English: to have Spanish: tenerno Incorrecto! [1] Try again. [2] Show answer. Enter option number: 2 The correct answer is ‘TENER’ Below are the word(s) that you missed. TENER – to have HABER – to exist (there is, there are)
Some CSV files to get you started: