In the original flashcard problem, a user can ask the program toshow an entry picked randomly from a glossary. When the userpresses return, the program shows the definition corresponding tothat entry. The user is then given the option of seeing anotherentry or quitting.
A sample session might run as follows:
Enter s to show a flashcard and q to quit: s Define: word1 Pressreturn to see the definition definition1 Enter s to show aflashcard and q to quit: s Define: word3 Press return to see thedefinition 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’ ora ‘hard’ glossary entry. If the user chooses to see an ‘easy’entry, the program picks an entry at random from the easy glossaryand shows the entry. After the user presses return, the programshould show the definition for that entry.
If the user chooses to see a ‘hard’ entry, the program picks anentry at random from the hard glossary and shows the entry. Afterthe user presses return, the program should show the definition forthat entry.
The user should be able to repeatedly ask for an easy or a hardentry or choose an option to quit the program.
A sample dialogue might run as follows. Changes from theoriginal flashcard program are indicated by underlining. word3 isfrom the ‘easy’ glossary, word6 from the ‘hard’ glossary.
Enter e to show an easy flashcard, h to show a hard one, and qto quit: e Define: word3 Press return to see the definitiondefinition3 Enter e to show an easy flashcard, h to show a hardone, and q to quit: h Define: word6 Press return to see thedefinition definition6 Enter e to show an easy flashcard, h to showa hard one, and q to quit: q
Box 2 – Keeping a notebook
As you work through part (a) of this question you should keep anotebook. You will need this for your answer to part (a)(vi). Thisshould be very brief: it is simply a record of yourpersonal experience while working on the task and what you feel youhave learned from it.
In your notebook we suggest that you record the followinginformation:
How | A brief description of how you went about the task. |
Resources | What documentation, if any, you consulted (including coursematerials 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 andsection or activity was. |
Difficulties | Anything you found difficult about the task, and how you dealtwith it. |
Lessons learnt | Anything you learned from the task that would be useful if youfaced a similar problem in the future. |
There is more than one way of solving the extended problem, butthe approach we ask you to follow for this TMA starts by addressingthe subproblem of showing a random entry from either the easy orthe hard glossary and, after the user enters return, showing thedefinition. The algorithm should select which glossary to usedepending on the user's input.
a.
- i.Begin by writing an algorithm for the subproblem, showdefinition, described in the middle paragraph of Box 1 above,and repeated here for convenience:
The program should allow the user to ask for either an easyor a hard glossary. If the user chooses to see an easy entry, theprogram picks an entry at random from the easy glossary and showsthe entry. After the user presses return, the program should showthe definition for that entry.
If the user chooses to see a hard entry, the program picksan entry at random from the hard glossary and shows the entry.After the user presses return, the program should show thedefinition for that entry.
At this stage, no looping is involved and the steps of thealgorithm only need to do what is asked for in the paragraph aboveand nothing more. Your algorithm will need to cater for the twovariables of the user asking for either an easy or a hardentry.
The steps of your algorithm must be written in English and notuse any Python code. The algorithm should be high-level and at asimilar level of detail to the solution to Activity 2.24 of Block 3Part 2, where an algorithm is given for showflashcard.
- ii.Next you will translate your algorithm into Pythoncode.
Modify the function show_flashcard() so it translates intoPython the steps of the algorithm you wrote in Part (i). You canassume the user's choice is stored in the variable user_input andis 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 intoyour Solution Document.
- iii.When you have modified the show_flash card() function, testit as follows.
Run the program and, when asked to make a choice, immediatelyenter q so the program quits.
Although the program has quit, the function is still loaded intomemory and can be called from the shell. To test it, first set thevalue 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 tosee 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 tomake 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. Thesecond 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 workingcorrectly, you should still paste in example tests and explainbriefly how the results are different from what you wereexpecting.
- iv.Now you need to make changes to the part of the program thatimplements the interactive loop, so the user is offered a choicebetween entering 'e' for an easy entry, 'h' for a hard entry, or'q' to quit.
If the user enters either ‘e’ or ‘h’, the show_flashcard()function should be called. The function will then show a randomentry from either the easy or hard glossary, depending on which ofthe two the user chose, which will have resulted in user_inputbeing 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 amessage reminding them what the possible options are.
Once you have made the changes, run the whole program. Copy atest dialogue into your Solution Document to show the user firstchoosing to see an 'easy' entry, then a 'hard' one, then enteringan invalid option, and finally entering 'q' to quit theprogram.
Alternatively, if you were unable to produce a test dialoguebecause you could not get the program to function as intended, youshould briefly explain how a successful test dialogue wouldlook.
- v.Next, modify the docstring for the program as a whole toreflect the changes you have made.