Question

In: Computer Science

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

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.

    • i.Begin by writing an algorithm for the subproblem, show definition, 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 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.

    • ii.Next you will translate your algorithm into Python code.
    • 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.

    • iii.When you have modified the show_flash card() function, test it as follows.

      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.

    • iv.Now you need to make changes to the part of the program that implements the interactive loop, so the user is offered a choice between 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 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.

    • v.Next, modify the docstring for the program as a whole to reflect the changes you have made.

Solutions

Expert Solution

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:


Related Solutions

Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Question: Conversion Program * This program will ask the user to input meters * After input,...
Question: Conversion Program * This program will ask the user to input meters * After input, a menu will be displayed: * 1. Convert to kilometers * 2. Convert to inches * 3. Convert to feet * 4. Quit * * The user will select the conversion and the converted answer will be displayed. * The program should redisplay the menu for another selection. * Quit program when user selects 4. Hi, my instructor requirement fix the bug in the...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
write a program i java that ask the user to enter salary, user ID and username...
write a program i java that ask the user to enter salary, user ID and username and out put them
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Write a program will ask the user for the annual income and the number of years...
Write a program will ask the user for the annual income and the number of years that the user has worked as their current job. The program will tell the user whether or not he or she qualifies for a loan based on the following condition: income is equal to or exceeds $35,000 or number of years on job is greater than 5 Do not use “nested if statements” in your solution. As an example of the output: What is...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT