0

I'm looking to create a Python based vocabulary checker for my little cousin to use for studying. The purpose of the program will be to display a word and then she will need to type in the definition and have it checked. I was wondering if the best way to do this is with array lists:

vocab = ['Python','OSX']
definition = ['programming language','operating system']

Is this the best way to go about this? And if so how do I have the program randomly display a vocab and then check the definition. Any help would be greatly appreciated. Thank you guys.

Ok. So this is what I have so far.... #Russian Translation Program

import os
import random

#Asks users if they want to add more vocabulary
word_adder=raw_input("Add more words? If yes, press 1: ")
with open("Russian_study.txt","a") as f:
while word_adder=="1":
    word=raw_input("Enter word: ")
    translation=raw_input("Word translation: ")
    f.write("'{0}':{1},".format(word,translation))
    word_adder=raw_input("Add another word? If yes, press 1: ")

#Checks to see if file exists, if not one is created
with open("Russian_study.txt","a") as f:
pass

os.system('clear')
print("Begin Quiz")

#Begin testing user
with open("Russian_study.txt","r") as f:
from random import choice
question = choice(list(f))
result = raw_input('{0} is '.format(question))
print('Correct' if result==f[question] else ':(')

However, my output is

Begin Quiz
'Один':'One', is 

How do I make it only display Один and check the user input against one?

h3tr1ck
  • 809
  • 2
  • 8
  • 16

2 Answers2

3

use a dictionary:

d={'Python':'programming language', 'OSX':'operating system'}

from random import choice
q = choice(list(d))
res = input('{0} is:'.format(q))
print('yay!' if res == d[q] else ':(')

[if you are using python < 3.0, use raw_input() instead of input()]

the simplest (and not safe!) way to write/read from a file:

with open('questions.txt', 'w') as f:
    f.write(repr(d))

'questions.txt' will have this line:

`{'Python':'programming language', 'OSX':'operating system'}`

so for reading it you can do

with open('questions.txt') as f:
    q=eval(f.read())

and now q and d are equal. don't use this method for "real" code, as 'questions.txt' may contain malicious code.

Elazar
  • 20,415
  • 4
  • 46
  • 67
  • As an additional feature, you might try to normalize the input. For example the last line could be `print('yay!' if res.strip().lower() == d[q] else ':(')` and so on, with other means of making it more user-friendly – jsvk May 08 '13 at 02:18
  • Thank you. If I were going to put the dictionary in a text file, how would it be formatted within the file. – h3tr1ck May 08 '13 at 03:34
  • I edited to read and write a file. if you wan to format it, look at `format` in the docs. – Elazar May 08 '13 at 11:10
0

1) You can use random.choice() to randomly pick an element from your vocab list (or the keys() of a dictionary).

2) Deciding when an answer is close enough to the definition is trickier. You could simply search the answer string for certain key words. Or if you want to get more complex, you can calculate something like the Levenshtein distance between two strings. You can read about the L distance here: http://en.wikipedia.org/wiki/Levenshtein%5Fdistance. And there are python recipes online for calculating the L distance.

7stud
  • 46,922
  • 14
  • 101
  • 127
  • If it's just a study aid, and isn't meant to grade someone, then it's sufficient to just display the definition after you type yours in. Then the studier can decide for themselves how close they were. – Patashu May 08 '13 at 02:08