In: Computer Science
In the original flashcard problem, a user can ask the program to show an entry picked randomly from a glossary. When the user presses return, the program shows the definition corresponding to that entry. The user is then given the option of seeing another entry or quitting.
A sample session might run as follows:
Enter s to show a flashcard and q to quit: s Define: word1 Press return to see the definition definition1 Enter s to show a flashcard and q to quit: s Define: word3 Press return to see the definition definition3 Enter s to show a flashcard and q to quit: q
The flashcard program is required to be extended as follows:
Box 1 – Specification of extended problem
There are now two glossaries: the ‘easy’ and the ‘hard’.
The program should allow the user to ask for either an ‘easy’ or a ‘hard’ glossary entry. If the user chooses to see an ‘easy’ entry, the program picks an entry at random from the easy glossary and shows the entry. After the user presses return, the program should show the definition for that entry.
If the user chooses to see a ‘hard’ entry, the program picks an entry at random from the hard glossary and shows the entry. After the user presses return, the program should show the definition for that entry.
The user should be able to repeatedly ask for an easy or a hard entry or choose an option to quit the program.
A sample dialogue might run as follows. Changes from the original flashcard program are indicated by underlining. word3 is from the ‘easy’ glossary, word6 from the ‘hard’ glossary.
Enter e to show an easy flashcard, h to show a hard one, and q to quit: e Define: word3 Press return to see the definition definition3 Enter e to show an easy flashcard, h to show a hard one, and q to quit: h Define: word6 Press return to see the definition definition6 Enter e to show an easy flashcard, h to show a hard one, and q to quit: q
Box 2 – Keeping a notebook
As you work through part (a) of this question you should keep a notebook. You will need this for your answer to part (a)(vi). This should be very brief: it is simply a record of your personal experience while working on the task and what you feel you have learned from it.
In your notebook we suggest that you record the following information:
How | A brief description of how you went about the task. |
Resources | What documentation, if any, you consulted (including course materials and any online sources) and which you found most useful. There is no need for full references, just note the source, and – in the case of the course materials – what the relevant part and section or activity was. |
Difficulties | Anything you found difficult about the task, and how you dealt with it. |
Lessons learnt | Anything you learned from the task that would be useful if you faced a similar problem in the future. |
There is more than one way of solving the extended problem, but the approach we ask you to follow for this TMA starts by addressing the subproblem of showing a random entry from either the easy or the hard glossary and, after the user enters return, showing the definition. The algorithm should select which glossary to use depending on the user's input.
a.
The program should allow the user to ask for either an easy or a hard glossary. If the user chooses to see an easy entry, the program picks an entry at random from the easy glossary and shows the entry. After the user presses return, the program should show the definition for that entry.
If the user chooses to see a hard entry, the program picks an entry at random from the hard glossary and shows the entry. After the user presses return, the program should show the definition for that entry.
At this stage, no looping is involved and the steps of the algorithm only need to do what is asked for in the paragraph above and nothing more. Your algorithm will need to cater for the two variables of the user asking for either an easy or a hard entry.
The steps of your algorithm must be written in English and not use any Python code. The algorithm should be high-level and at a similar level of detail to the solution to Activity 2.24 of Block 3 Part 2, where an algorithm is given for show flashcard.
Modify the function show_flashcard() so it translates into Python the steps of the algorithm you wrote in Part (i). You can assume the user's choice is stored in the variable user_input and is either 'e' or 'h' for easy or hard respectively.
Make sure you write a suitable docstring for the function.
Copy your modified show_flashcard() function and paste it into your Solution Document.
Run the program and, when asked to make a choice, immediately enter q so the program quits.
Although the program has quit, the function is still loaded into memory and can be called from the shell. To test it, first set the value of user_input to 'e'
>>> user_input = 'e'
Now call the function
>>> show_flashcard()
If the function is correct, this should display one of the ‘easy’ words: word1, word2 or word3, followed by Press return to see the definition.
Repeat this process but this time set the value of user_input to 'h' and check that now the word displays one of the 'hard' words: word4, word5 or word6.
Debug the code and/or algorithm as necessary. If you need to make modifications, you should record them in your notebook.
Copy and paste two example tests into your Solution Document. The first test should show the result of user_input being set to 'e', the function being called, and the user pressing return. The second test should show the result of user_input being set to 'h', the function being called, and the user pressing return.
Alternatively, if you were unable to get the function working correctly, you should still paste in example tests and explain briefly how the results are different from what you were expecting.
If the user enters either ‘e’ or ‘h’, the show_flashcard() function should be called. The function will then show a random entry from either the easy or hard glossary, depending on which of the two the user chose, which will have resulted in user_input being set to the corresponding value.
If the user enters ‘q’, the program should quit as before.
If the user enters anything else, the program should print a message reminding them what the possible options are.
Once you have made the changes, run the whole program. Copy a test dialogue into your Solution Document to show the user first choosing to see an 'easy' entry, then a 'hard' one, then entering an invalid option, and finally entering 'q' to quit the program.
Alternatively, if you were unable to produce a test dialogue because you could not get the program to function as intended, you should briefly explain how a successful test dialogue would look.
from random import *
def show_flashcard():
""" Show the user a random key and ask them
to define it. Show the definition
when the user presses return.
"""
glossary = {}
if user_input == 'e':
glossary = easy_glossary
elif user_input == 'h':
glossary = hard_glossary
else:
return
random_key = choice(list(glossary))
print('Define: ', random_key)
input('Press return to see the definition')
print(glossary[random_key])
# Set up the glossary
easy_glossary = {'word1':'definition1',
'word2':'definition2',
'word3':'definition3'}
hard_glossary = {'word4':'definition4',
'word5':'definition5',
'word6':'definition6'}
# The interactive loop
exit = False
user_input = 'e'
while not exit:
user_input = (input("Enter 'e' for an easy entry, 'h' for a hard
entry, or 'q' to quit: "))
if user_input == 'q':
print("Thanks for using the program!")
exit = True
elif user_input == 'e' or user_input == 'h':
show_flashcard()
else:
print('You need to enter either e, h or q')
output: