Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the original flashcard problem, a user can ask the program to show an entry p

ID: 3745174 • Letter: I

Question

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 could 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

After every definition is shown, the user is asked whether they knew that definition. If they answer “yes”, the program informs them that the entry will not be shown again. It then removes that entry from the glossary and tells the user how many entries remain. 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 and q to quit: s

Define: word3

Press return to see the definition

definition3

Did you know the definition? Enter y for yes: n

Enter s to show a flashcard and q to quit: s

Define: word3

Press return to see the definition

definition3

Did you know the definition? Enter y for yes: y

Now you know the definition that flashcard will be removed

2 cards remain

Enter s to show a flashcard 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

There is more than one way to solve the extended problem but the approach we ask you to follow begins by modifying the part of the program that shows the flashcard.

a.

i.The algorithm developed for this in Activity was as follows.

   

>> show flashcard

Choose a random entry from the glossary

Print this entry to the screen

In response to the user pressing return, print the definition of the entry

Begin by copying this algorithm. Then add the further steps needed to solve the extended problem specified in Box 1. All the steps you add should follow the existing ones. Your additional steps should be written in English and not use any Python code. Information on how to delete a dictionary entry in Python is given later, but not needed to answer this part.

Note that Box 1 ignores the possibility of the glossary becoming empty and your algorithm should do the same for now. How to deal with the possibility of an empty glossary will be considered in Part (iv).

ii.Translate your algorithm into Python code. Use the first complete version of the flashcard program:

"""
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.')

Save a copy of the Python 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 instruction in Part v.).

The function show_flashcard() shows the flashcards, this is where you should make all the changes.

Translating the algorithm into Python code requires entries to be removed from the glossary when appropriate. Use the following information about the Python deloperation to remove entries from a Python dictionary.

   

Box 3 – The Python del operation.

A dictionary entry can be removed using the Python del operation.

If sample_key is a key in the dictionary sample_dictionary, then the entry for sample_key can be removed as follows:

del sample_dictionary[sample_key]

In this example, using the shell, key entry 2 is removed from my_dictionary:

>>> my_dictionary = {1:'item 1', 2: 'item 2', 3: 'item 3'}

>>> my_dictionary

{1: 'item 1', 2: 'item 2', 3: 'item 3'}

>>> del my_dictionary[2]

>>> my_dictionary

{1: 'item 1', 3: 'item 3'}

iii.When you have modified the show_flashcard() function test it by calling it several times in the shell. Remember to first run the program and only after that use the shell to call the function.

Debug the code and/or algorithm as necessary. If you need to make modifications you should record them in your notebook.

Once you have the function working correctly, run the whole program and test it with a variety of user inputs. Copy a test dialogue into your Solution document that shows the user answering that they have seen a definition before and the program responding appropriately.

iv.Once you have the show_flashcard() function working correctly you can return to the issue of what should happen if the glossary becomes empty. This can be addressed by a single change. This change is to the part of the program that implements the interactive loop (not to the show_flashcard() function). Make this change to the part of the program that implements the interactive loop, and then run the program again to test it. Copy a test dialogue that shows the program quitting when the number of remaining entries reaches zero, and paste it into your Solution Document.

v.Next make changes to the docstrings for the function and for the program as a whole, to reflect the changes.

vi.Also copy the notebook you have kept for this question into the corresponding part of your Solution Document.

19 marks

b.Suggest one further small extension or improvement of your own to the flashcard program. Outline what the extension does and include any additional algorithm step(s). 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 the added algorithm steps.

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.

Explanation / Answer

""" 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[0]] = 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()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote