patternpythonMinor
Python Word definition practice
Viewed 0 times
definitionwordpracticepython
Problem
I've written this script to practice (not necessarily memorize, but familiarize myself with) the definitions of a lot of 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:
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:
Solution
In python it is usually frowned upon iterating with:
and it is preferred to iterate over the iterable directly:
Similarly, it is a lot easier to use
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
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
I also added
Final code:
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 xSimilarly, 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:
words = list(csv.reader(csvfile))
score = 0
count = 1
while count <= WORDS:
print("\nQuestion {}".format(count))
word, correct_answer = random.choice(words)
print("Word: {}".format(word))
answers = [correct_answer]
while len(answers) < CHOICES:
_, random_answer = random.choice(words)
if random_answer not in answers:
answers.append(random_answer)
random.shuffle(answers)
correct_index = answers.index(correct_answer) + 1
for choice, answer in enumerate(answers, start=1):
print("{}: {}".format(choice, answer))
userinput = 0
while not 0 < userinput <= CHOICES:
try:
userinput = int(input("Answer: "))
except ValueError:
continue
if userinput == correct_index:
score += 1
print("Correct")
else:
print("Wrong: {}".format(correct_answer))
count += 1
print("{}/{}".format(score, WORDS))Code Snippets
l = [1, 2, 3]
for i in range(len(l)):
print l[i]l = [1, 2, 3]
for x in l:
print ximport csv
import random
WORDS = 5 # Number of words
CHOICES = 5 # Number of choices
with open("wordlist.csv") as csvfile:
words = list(csv.reader(csvfile))
score = 0
count = 1
while count <= WORDS:
print("\nQuestion {}".format(count))
word, correct_answer = random.choice(words)
print("Word: {}".format(word))
answers = [correct_answer]
while len(answers) < CHOICES:
_, random_answer = random.choice(words)
if random_answer not in answers:
answers.append(random_answer)
random.shuffle(answers)
correct_index = answers.index(correct_answer) + 1
for choice, answer in enumerate(answers, start=1):
print("{}: {}".format(choice, answer))
userinput = 0
while not 0 < userinput <= CHOICES:
try:
userinput = int(input("Answer: "))
except ValueError:
continue
if userinput == correct_index:
score += 1
print("Correct")
else:
print("Wrong: {}".format(correct_answer))
count += 1
print("{}/{}".format(score, WORDS))Context
StackExchange Code Review Q#140843, answer score: 3
Revisions (0)
No revisions yet.