Question: based on the below algorithm fill in the rest of the below code Choose a random glossary key Choose another random glossary key Decide randomly
based on the below algorithm fill in the rest of the below code
Choose a random glossary key
Choose another random glossary key
Decide randomly whether to display the correct or incorrect definition
Display the random glossary key
Display either the correct or incorrect definition, as appropriate
Ask the user if key and the definition is correct
If correct definition was displayed and user evaluate it as correct, display user answer is correct
If incorrect definition was displayed and user evaluate it as incorrect, display user answer is correct
else display user answer is incorrect
repeat until user quits
Python code:
def show_flashcard():
"""
ADD YOUR DOCSTRING HERE
"""
""" Do not change this first section."""
# Get glossary keys.
keys = list(glossary)
# Choose two distinct keys at random.
sample_keys = sample(keys, 2)
# The first is the entry that will be displayed.
# The definition corresponding to this is the right one.
random_entry = sample_keys[0]
# The second entry will not be displayed.
# The definition corresponding to this is the wrong one.
other_random_entry = sample_keys[1]
# This variable will control whether the user is
# shown the right definition or the wrong one.
right_or_wrong = choice(['right', 'wrong'])
# Pose question.
print('Here is a glossary entry:')
print(random_entry)
print('Here is a possible definition for it:')
if right_or_wrong == 'right':
print(glossary[random_entry])
else:
print(glossary[other_random_entry])
""" End of first section."""
""" Insert your code below."""
""" Do not change anything beyond this point """
# Set up the glossary
glossary = {'word1':'definition1',
'word2':'definition2',
'word3':'definition3'}
# The interactive loop
exit = False
while not exit:
user_input = input('Enter s to show a flashcard and q to quit: ')
if user_input == 'q':
exit = True
elif user_input == 's':
show_flashcard()
else:
print('You need to enter either q or s.')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
