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:
wordreader = csv.reader(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])
correct_answer = definitions[randomnumber]
answers = []
answers.append(correct_answer)
for i in range(len(CHOICES)-1):
randomanswer = random.randint(0, len(words)-1)
if definitions[randomanswer] in answers:
randomanswer -= 1
answers.append(definitions[randomanswer])
answers.sort()
correct_index = answers.index(correct_answer)
for i in range(len(CHOICES)):
print(CHOICES[i] + ": " + answers[i])
userinput = input("Answer: ")
if userinput[0].upper() in CHOICES:
userindex = CHOICES.index(userinput)
if userindex == correct_index:
score += 1
print("Correct")
else:
print("Wrong: " + correct_answer)
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
A: Adapt
B: Forgiveness of their sins
C: Stress
D: Superintendent of a monastery
E: Unreasonable
Answer: C
Correct
Question 2
Word: ackja
A: Sami sledge
B: Singing without instruments
C: Speech problems
D: Superintendent of a monastery
E: Unreasonable
Answer: A
Correct
Question 3
Word: a cappella
A: Adapt
B: Adapt
C: Appropriate
D: Forgiveness of their sins
E: Singing without instruments
Answer: E
Correct
Question 4
Word: adequate
A: Adapt
B: Appropriate
C: Forgiveness of their sins
D: Intangible
E: Sami sledge
Answer: B
Correct
Question 5
Word: ackja
A: Adapt
B: Forgiveness of their sins
C: Sami sledge
D: Speech problems
E: Stress
Answer: C
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
adequate,Appropriate
accommodate,Adapt