In: Computer Science
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 of seeing a definition first.
If the user chooses this option, the program picks an entry at random from the glossary and shows the definition for that entry. It then asks the user what word is being defined. When the user presses return the program shows the word concerned.
The user is then given the option of seeing another flashcard, seeing another definition, or quitting.
Apart from this the program behaves like the original version
A sample dialogue might run as follows: (the additional dialogue is underlined.)
Enter s to show a flashcard, d to see a definition, or q to quit: s Define: word3 Press return to see the definition definition3 Enter s to show a flashcard, d to see a definition, or q to quit: d What word is defined by: definition2 Press return to see the word word2 Enter s to show a flashcard, d to see a definition, 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 showing the definition for a random entry and, after the user enters return, showing the word being defined.
a.
… the program picks an entry at random from the glossary and shows the definition for that entry. It then asks the user what word is being defined. When the user presses return the program shows the word concerned.
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.
Begin with the first complete version of the flashcard program, a copy of which is included in the download for this TMA as Q2.py. Save a copy this program as Q2_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 v).
Add a new function show_definition() 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.
Include your code that defines the function show_definition() in your Solution document.
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 demonstrate a definition being shown, the user being asked to enter return, and then the program showing the word concerned.
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.
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 see a definition, then being asked what word it defines, and then been shown the word concerned.
Include your amended code for the interactive loop in your Solution document.
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.
Save your final version of the Python program and submit it as Q2_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.
code:
""" add docstring info here to describe what program does"""
from random import *
import csv
def file_to_dictionary(filename):
""" Return a dictionary with the contents of a file"""
file = open(filename, ‘r’)
reader = csv.reader(file)
dictionary = {}
for row in reader:
dictionary[row[O]] =
row[1]
return dictionary
def show _flashcard():
key _list =
list(glossary)
random_key = choice(key
_list)
print (random
_key)
input("")
print
(glossary[random_key])
print ("")
correct = input("Did you
know the definition? (Enter y for yes)")
if correct == "y":
print ("Now you know the definition that flashcard will be
removed."}
del glossary[random_key]
dict_length = len(glossary)
print (str(dict_length) + ' cards remain.')
glossary = file_to_dictionary('TM112_Glossary. txt')
exit = False
while exit == False:
user_input =
input("Enter s to show flashcard and q to quit > ")
if user_input ==
"q":
exit = True
elif user_input ==
“s":
show _flashcard()