h3tr1ck
h3tr1ck

Reputation: 909

Python Vocab Checker

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?

Upvotes: 0

Views: 1416

Answers (2)

Elazar
Elazar

Reputation: 21615

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.

Upvotes: 3

7stud
7stud

Reputation: 48599

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.

Upvotes: 0

Related Questions