# Python Word definition practice

I've written this script to practice (not necessarily memorize, but familiarize myself with) the definitions of a lot of words.

import csv
import random

WORDS = 5    # Number of words
CHOICES = ["A", "B", "C", "D", "E"]    # Number and labelling of choices

with open("wordlist.csv") as csvfile:
wordlist = [row for row in wordreader]
words = [pair[0] for pair in wordlist]
definitions = [pair[1] for pair in wordlist]

score = 0

count = 1
while count <= WORDS:
print("\nQuestion", str(count))
randomnumber = random.randint(0, len(words)-1)
print("Word:", words[randomnumber])

for i in range(len(CHOICES)-1):

for i in range(len(CHOICES)):
print(CHOICES[i] + ": " + answers[i])

if userinput[0].upper() in CHOICES:
userindex = CHOICES.index(userinput)
if userindex == correct_index:
score += 1
print("Correct")
else:

count += 1

print(str(score) + "/" + str(WORDS))


Sample output:

Python 3.4.5 (default, Jul 03 2016, 13:55:08) [GCC] on linux-027e.site, Standard
>>>
Question 1
Word: accentuate
B: Forgiveness of their sins
C: Stress
D: Superintendent of a monastery
E: Unreasonable
Correct

Question 2
Word: ackja
A: Sami sledge
B: Singing without instruments
C: Speech problems
D: Superintendent of a monastery
E: Unreasonable
Correct

Question 3
Word: a cappella
C: Appropriate
D: Forgiveness of their sins
E: Singing without instruments
Correct

Question 4
B: Appropriate
C: Forgiveness of their sins
D: Intangible
E: Sami sledge
Correct

Question 5
Word: ackja
B: Forgiveness of their sins
C: Sami sledge
D: Speech problems
E: Stress
Correct
5/5


Note that there shouldn't be duplicate questions, it's just more likely with a short wordlist like the one at the end of this post and the current implementation. Also, the same definitions shouldn't appear twice in the same question (like in Question 3) My solution to that seems to work most of the time, it's unclear to me why it doesn't work for Question 3 above.

There are a number of things that I'm wondering about:

• Use of random.sample to choose words and answers, it doesn't seem like this will make the code any shorter or concise.
• I'm not quite satisfied with the code for choosing the random answers, it seems to be too complicated, as well as the code for checking the user answer. Is there an easier way to verify that the chosen letter is the one that stands for the correct definition?
• How could I eliminate the chance of the same word being asked twice in one run of the script?

Here is a sample wordlist.csv, where I've translated 10 of the ca. 3500 original entries:

a cappella,Singing without instruments
absurd,Unreasonable
abbot,Superintendent of a monastery
accentuate,Stress
Aphasia,Speech problems
ackja,Sami sledge
abstrakt,Intangible
absolution,Forgiveness of their sins

• Some words you are more familiar with, some are less - your program does not take that into account. Using spaced repetition would be more effective, you could look at Anki's source for inspiration. – Daerdemandt Sep 8 '16 at 15:15
• @Daerdemandt Indeed, but I didn't really want to make it as complicated. It's a list of around 3.5k words after all, and it's only revision for a word test of 40 random words (some of which may not even be in my practice list) :) – Dartmouth Sep 8 '16 at 16:00

In python it is usually frowned upon iterating with:

l = [1, 2, 3]
for i in range(len(l)):
print l[i]


and it is preferred to iterate over the iterable directly:

l = [1, 2, 3]
for x in l:
print x


Similarly, it is a lot easier to use random.sample (or random.choice if you don't care if a word is asked multiple times) to choose a random word to test now.

Also, it is easier to leave the word and its definition bundled in a tuple, because most of the time you need both of them.

Your choices included sometimes multiple times the same words, because you check only once whether the random answer is already included (if it is, you choose randint - 1, which might also already be included). You should keep on polling until you find a word not included in answers yet (This assumes your dictionary to have more words than CHOICES).

Similar for your user choice. It is nicer (to the user) to keep on asking for an answer until he answers with a possible choice.

I made you enumeration of the choices to be numbers rather than letters because it makes it easier to validate the correct answer.

You should in general use python's str.format methods rather then string addition. It is usually faster and also more readable (and extendable, you can add text justification, padding, etc).

I also added random.shuffle(answers) instead of answer.sort() just so it does not get too boring.

Final code:

import csv
import random

WORDS = 5    # Number of words
CHOICES = 5    # Number of choices

with open("wordlist.csv") as csvfile:

score = 0
count = 1

while count <= WORDS:
print("\nQuestion {}".format(count))
print("Word: {}".format(word))

userinput = 0
while not 0 < userinput <= CHOICES:
try:
except ValueError:
continue
if userinput == correct_index:
score += 1
print("Correct")
else:

• The problem is that csv.reader returns each line as a list, not a tuple. So the first word/definition pair (first item in words) actually looks like this: ['a cappella', 'Singing without instruments']. So unpacking raises an error, because there is only 1 item. – Dartmouth Sep 8 '16 at 13:52
• ah, partially my mistake, I was looking at the wrong unpacking... The error I'm getting is on line 16: word, correct_answer = random.sample(words, 1), which gives me: Exception "unhandled ValueError". need more than 1 value to unpack. This unpacking doesn't work, because random.sample(…) returns a nested list like [['absurd', 'Unreasonable']] – Dartmouth Sep 8 '16 at 14:08
• Putting a [0] at the end of lines 16 and 21 (where the same problem exists) solves this though. – Dartmouth Sep 8 '16 at 14:11
• Instead of [x for x in ...] you could also write list(...) directly. – ferada Sep 8 '16 at 19:00