Question
Question 3 This question is concerned with an extension to the flashcard problem you studied in Block 3 Part 2. In the original flashcard problem,
Question 3
This question is concerned with an extension to the flashcard problem you studied in Block 3 Part 2.
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
As well as being offered a choice between seeing a flashcard or quitting, the user is offered the option to add a new glossary entry.
If the user picks this option they are first asked to enter the new word, and then the corresponding definition. The program then adds the word and definition to the glossary as a new entry and informs the user that this has been done.
Apart from these differences the program behaves like the original version.
A sample dialogue could run as follows. The additional dialogue is underlined.
Enter s to show a flashcard, a to add a new card, or q to quit: s
Define: word3
Press return to see the definition
definition3
Enter s to show a flashcard, a to add a new card, or q to quit: a
Enter the new word: some word
Enter the definition: some definition
New entry added to glossary
Enter s to show a flashcard, a to add a new card, or 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 sub-problem of adding a new glossary entry.
-
a.
- i.Begin by writing an algorithm for the subproblem, add flashcard, which carries out the steps necessary if the user picks the option of adding a single new glossary entry, as described in Box 1 above, and repeated here for convenience.
they are first asked to enter the new word, and then the corresponding definition. The program then adds the word and definition to the glossary as a new entry and informs the user that this has been done.
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.
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.
- ii.Next you will translate your algorithm into Python code.
Begin with the first complete version of the flashcard program, a copy of which is included in the download for this TMA as Q3.py. Save a copy this program as Q3_OUCU.py (where OUCU is your OU computer username, e.g. abc123).
In the next few question parts, you will be amending this file. You will only have to submit the final amended file (as per the instructions in Part vii).
Add a new function add_flashcard() to the program, which translates into Python the steps of the algorithm you wrote in part i. You should insert the new function just after the show_flashcard() function.
Make sure you write a suitable docstring for the function.
How to add a new item (i.e. an entry) to a dictionary, given a key and a value, is discussed in Subsection 2.1.4 of Block 2 Part 2, and repeated below for convenience.
- i.Begin by writing an algorithm for the subproblem, add flashcard, which carries out the steps necessary if the user picks the option of adding a single new glossary entry, as described in Box 1 above, and repeated here for convenience.
Box 3 Adding an item to a dictionary
Given a dictionary sample_dictionary, new key sample_key and a new value sample_value, we can add a new entry to the dictionary by using an assignment
sample_dictionary[sample_key] = sample_value
-
- iii.When you have modified the add_flashcard() function test it by calling it several times in the shell. Remember to first run the program and only afterthat use the shell to call the function.
Once you have called the function, provided the required inputs, and have been informed that the new entry has been added, you can confirm that the new entry really has been added by entering the name of the dictionary, glossary, and the contents will be displayed.
Debug the code and/or algorithm as necessary. If you need to make modifications you should record them in your notebook.
Copy and paste an example test into your Solution Document. This should show a new entry being added and confirmation that the action was successful.
Alternatively, if you were unable to get the function working correctly, you should still paste in an example test, and explain briefly how the results are different from what you were expecting.
- iv.Now you need to make changes to the part of the program that implements the interactive loop, so the user is offered the option of adding a new glossary entry, and if they choose this option the add_flashcard() function is called.
Once you have made the changes, run the whole program. Copy a test dialogue into your Solution document to show the user selecting the option to add a new entry, then being prompted for the word and its definition, then being informed that the new entry has been added.
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.
- v.Next modify the docstring for the program as a whole to reflect the changes you have made.
Save your final version of the Python program and submit it as Q3_OUCU.py (where OUCU is your OU computer username, e.g. abc123) in your TMA zip file.
Also paste a copy of your final Python program into your solution document as text.
- vi.Finally, copy the notebook you have kept for this question into the corresponding part of your Solution Document.
(19 marks)
- iii.When you have modified the add_flashcard() function test it by calling it several times in the shell. Remember to first run the program and only afterthat use the shell to call the function.
- b.Suggest one further small extension or improvement of your own to the modified flashcard program. Outline what the extension does and include any additional algorithm step(s) needed, either in the functions, or in the interactive loop. You are not required to implement the code, although of course you may do so if you choose. Your answer to this part should be no longer than 150 words, including any added algorithm steps.
(4 marks
-
# Q3.py # TMA03 18J Question 3, Part (a), ii, iv, and vi """ This flashcard program allows the user to ask for a glossary entry. In response, the program randomly picks an entry from all glossary entries. It shows the entry. After the user presses return, the program shows the definition of that particular entry. The user can repeatedly ask for an entry and also has the option to quit the program instead of seeing another entry. """ 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. """ random_key = choice(list(glossary)) print('Define: ', random_key) input('Press return to see the definition') print(glossary[random_key]) # 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started