Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fundamentals covered in the first half of the unit. Assignment Overview You are required to design and implement two related programs: - "admin.py", a CLI program that allows the user to add, list, search, view and delete details of famous quotes, and stores the data in a text file. Develop this program before "quotes.py". - "quotes.py", a GUI program that uses the data in the text file to display the quotes and allows the user to rate them. Develop this program after "admin.py". The following pages describe the requirements of both programs in detail. Starter files for both programs are provided along with this assignment brief to help you get started and to facilitate an appropriate program structure. Please use the starter files. Pseudocode As emphasised by the case study of Module 5 , it is important to take the time to properly design a solution before starting to write code. Hence, this assignment requires you to write and submit pseudocode of your program design for "admin.py", but not "quotes.py" (pseudocode is not very well suited to illustrating the design of an event-driven GUI program). Furthermore, while your tutors are happy to provide help and feedback on your work throughout the semester, they will expect you to be able to show your pseudocode and explain the design of your code. You will gain a lot more benefit from pseudocode if you actually attempt it before trying to code your program - even if you just start with a rough draft to establish the overall program structure or a single section of the program, and then revise and expand it as you work on the code. This back-and-forth cycle of designing and coding is completely normal and expected, particularly when you are new to programming. The requirements detailed on the following pages should give you a good idea of the structure of the program, allowing you to make a start on designing your solution in pseudocode. See Reading 3.3 and the Rubric and Marking Criteria document for guidance regarding pseudocode. Write a separate section of pseudocode for each function you define in your program so that the pseudocode for the main part of your program is not cluttered with function definitions. Ensure that the pseudocode for each of your functions clearly describes the parameters that the function receives and what the function returns back to the program. Pseudocode for functions should be presented after the pseudocode for the main part of your program. It may help to think of the pseudocode of your program as the content of a book, and the pseudocode of functions as its appendices: It should be possible to read and understand a book without necessarily reading the appendices, however they are there for further reference if needed. The functions required in "admin.py" are detailed later in the assignment brief. Overview of "admin.py" "admin.py" is a program with a Command-Line Interface (CLI) like that of the programs we have created throughout the majority of the unit. The program can be implemented in under 200 lines of code (although implementing optional additions may result in a longer program). This number is not a limit or a goal - it is simply provided to prompt you to ask your tutor for advice if your program significantly exceeds it. Everything you need to know to develop this program is covered in the first 7 modules of the unit. This program should be developed before "quotes.py". This program allows the user to manage a collection of quotes that is stored in a text file named "data.txt". Use the "j son" module to write data to the text file in JSON format and to read the JSON data from the file back into Python. See Reading 7.1 for details of this. To illustrate the structure of the data, below is an example of the file content in JSON format: This example data contains the details of three quotes in a list. Each one is a dictionary containing of 4-5 key-value pairs with keys of: - "quote" - "author" - "year" (optional) - "likes" - "loves" The "likes" and "loves" keys contain integers and are used to record the ratings that the quote has received when shown to the user in the "quotes.py" GUI program. Parts of the "admin.py" program interact with the "likes" and "loves" values, however their values only increase via "quotes.py" (although you can manually edit the values in the text file to test the code of your program). If this file was to be read into a Python variable named data, then data [0] would refer to the dictionary containing the quote by Shakespeare, and data [0][ 'year' ] would refer to the string of "1602". data [2] ['loves'] would refer to the integer of 5. Understanding the structure of this data and how to interact with it is very important in many aspects of this assignment - in particular, you will need to understand how to loop through the items of a list and how to refer to items in a dictionary. Revise Module 3 and Module 7 if you are unsure about how to interact with lists and dictionaries, and see the Discussions on the unit site for further help. Output Example of "admin.py" To help you visualise the program, here is an example screenshot of the program being run: Melcone to the "puote Catalogue" Adnin progrant. Chopme [aldd, [1]int, [wlearch, [v]iow, [d] lete or laluit. We enter "lo to list the current 1 quotes, and are told there are none. Fhere are no uotes saved. Choose [a]dd, [1]ine, [e]earch, tv]lev, [d]ezoze or-Ig]uit. 34 Enter the quote: Brevity in the seal of wit. We enter "a" to add a new quote, Enter the author" 5 nane: IIIIam shakespeare entering the quote, author and yeor Enter the yoar (Leave blank if unknown) : 1602 when prompted to do so. Choons (a)dd, [1]ist, [s]earch, [v]iew, [d]elote or [q]uit. 3 a Enter the gaote: From caring cones courage. We repeat the process to odd a Entee the authoz" s nane: Lao Tzu Entey che yoar (Leave hiank if unknons) = second quote, with no year. quote addeat. Choose tajdd, fliste [s]eacch, tr)iew, [djelete or [giuit. And a third. Entex the quote: How Can Miryors be Meal If our eysa kren"t Baat. The colour of your input changes if you use Enter the authoz "a name J Jaden gaith words or symboks that Python recognses, but Mntef the pear (2esve blank if uriknewn) = 2019 this has no impoct uponionything. Quote addedt choose [a]od, [1]1et, [e]earch, [v]lev, [d]olote or [q]utt. >1 Now when we enter "f to list, Iise of quoten: we con see the three quotes. 2) "Drevity in the noul of wit, - Willian shakespoare, 1602 Long quoteshave beentruncoted, 3) Hon Can Mirrors Bo Real If Gar Eyes...7 - Jaden Bnith, 2013 and the yeor is omitted if unknown. Choore talda, [1]iat, [a]earchi, [v]lav, [d]elate or [e] uit. 3.d We enter "a" to delete a quote. Quiote number to delete; 3 orote deletedl speclfying the number to delete. Choose [a]ed, [1]lat, [e]earch, tvilew, Id]elete or [g]u1t. 21 Ii et of quotes = As you con see, the third quote has 11 "Brevity is the soul of wit." - Willian shakespeare. 1603 been deleted. 2) "Eron earing doales courage." - Lao Tzu Choose [a]da, [1]ler, [s]eatch, fvlieve [d]eleze or le] uit. 3 v Quote number to ries: 1 We enter "vo to wew a quote. "Brevity ie the sonl of wit," specifying the number to view, - Wilitan thakespesre, 1602 and are shown all of its details. this quoee has recelved 0 Iiken and 0 loves. Choose fajdd, [1)ist, [s]earch, [vlien, [d]elete or [e]uit, >5 Entering "s" allows us to seorch the Enter a seazch tors: tzu quotes, only listing ones that Beareh resulter contain the search term. 2) "reon eating ecmea courage." = Las Ttu Choose faldd, \{1)ist, [s]earch, tvliew, td]elete or tg]uit, >q Entering " q " ends the progrom by Goodioyol breaking out of the menu loop. you do not understand the requirements or would like further information. The requirements are: 1. The first thing the program should do is try to open a file named "data.txt" in read mode, then load the data from the file into a variable named data and then close the file. - The dota in the file should be in ISON format, so you will need to use the "10ad () "function from the "Ison" madule to read the dato into your program. See the earlier page for details of the structure. If any exceptions occur (e.g. due to the file not existing, or it not containing valid ISON dota), then simply set the da ta variable to be an empty list. This will occur the first time the program is run, since the data file will not exist yet. This ensures that you are always left with a list named data. - This is the first and only time that the program should need to read anything from the file. After this point, the program uses the di ta variable, which is written to the file whenever a change is made. 2. The program should then print a welcome message and enter an endless loop which starts by printing a list of options: "Choose [a]dd, [1]ist, [s]earch, [v]iew, [d]elete or [q]uit." and then prompts the user to enter their choice. Once a choice has been entered, use an "if/elif" statement to handle each of the different choices (detailed in the following requirements). This requirement has been completed for you in the starter file. 3. If the user enters " a " (add), prompt them to enter a quote, followed by its author and year. Place the details into a new dictionary with the keys shown earlier, and append the dictionary to the data list. Finally, write the entire data list to the text file in JSON format to save the data. - Use your "Input_something (1) "function (detailed below) when prompting the user for the quote and outhor, to ensure that they are re-prompted until they enter something other than whitespoce. - When prompting for the year, specify how to indicate if it is unknown and do not include the "year" key in the dictionary if the user indicates that it is unknown. - Remember to include the "likes" and "loves" keys in the dictionary (they should have values of o). - Once the new dictionary has been appended to the data list, coll your "save data () function (detalled below) to write the dato to the text file in ISOW format. 4. If the user enters " (list), display the details of all quotes in the data list preceded by their index number plus 1 (so that the list starts from 1 rather than 0 ). - If the da ta list is empty, show a "No quotes saved" message instead. - Use the "abbreviated" format to display quotes in this port of the program (detoiled below). - Use a "for"loop to iterate through the items in the da za list. Remember: each item is a dictionary. - You can use the "enumerate ( ) "function to ensure that you hove access to voriables containing the index number and dictionary of each item os you loop through them (see Lecture 3). 5. If the user enters "s" (search), prompt them for a search term and then list the details of all quotes which contain the search term in the quote text or author name. Include the index number plus 1 next to each result. - If the da ta list is empty, show a "No quotes saved" message instead of prompting for a seorch term. - Use the "abbreviated" format to display quotes in this part of the program (detailed below). - Use your "input_something (1)" function (detoiled below) when prompting the user for a search term, to ensure that they are re-prompted untll they enter something other than whitespace. - The code to search will be similar to the cade used to list, but this time the loop body needs an " f " stotement to only list quotes that contain the search term (use the "in" operator-see Lecture 3). - Ensure that the seorching is not case-sensitive, e.g. "tzu" should find quotes by "Loo Tzu". 6. If the user enters " v " (view), prompt them for an index number and then print details of the corresponding quote. Include the quote's number of likes and loves beneath the quote details. - If the da ta list is empty, show a "No quotes saved" message instead of prompting for index number. - Use the "full" format to display the quote in this part of the program (detailed below). - Use your "input_int (1) function (detaliled below) when prompting for an index number, to ensure that the user is re-prompted until they enter an integer. - Remember: Index numbers shown to/entered by users stort from 1 , but the da ta list starts from 0 . - Print an "Invalid index number" messoge if the index number entered does not exist in the da ta list. 7. If the user enters " d " (delete), prompt them for an index number and then delete the corresponding quote's dictionary from the data list, then print a confirmation message. - If the da ta list is empty, show a "No quotes saved" message instead of prompting for index number. - Use your "input_int (1" function (detailed below) when prompting for an index number, to ensure that the user is re-prompted until they enter an integer. - Remember: Index numbers shown to/entered by users start from 1, but the da ta list starts from 0 . - Print an "Invalid index number" messoge if the index number entered does not exist in the da ta list. - Once the quote has been deleted from the data list, call your "save_data ()" function (detailed below) to write the dato to the text file in ISOW format. 8. If the user enters "q" (quit), print "Goodbye!" and break out of the loop to end the program. 9. If the user enters anything else, print an "Invalid choice" message (the user will then be reprompted for a choice on the next iteration of the loop). This concludes the core requirements of "admin.py". The following pages detail the functions mentioned above and optional additions and enhancements that can be added to the program. Remember that you are required to submit pseudocode for your design of "admin.py". Use the starter file and requirements above as a guide to structure the design of your program. Formatting of Quotes in "admin.py" Quotes are displayed in three separate parts of the admin program: List, Search and View. When listing or searching quotes, they should be presented in the following "abbreviated" format: index) "abbreviated quate..." - author, year e.g. 4) "The great thing about getting older..." - Vadeleine L'Engle, 1985 In the abbreviated format, all details of a quote are presented on a single line. The number the quote (its index in the data list, plus 1) should be included at the beginning, followed by the quote text with double quote marks around it. If the quote is longer than 40 characters, it should be truncated so that it fits into 40 characters, with "..." added to the end. The "..." should only appear if the quote is over 40 characters long. The "textwrap.shorten()" function from the "textwrap" module can help you to do this. Following the quote, there should be a dash followed by the author name and year. When viewing a single quote, it should be presented in the following "full" format: "quote" - author, year e.g. "Brevity is the soul of wit." - William Shakespeare, 1602 You do not need to worry about the length of the quote when using the full format - if a quote is longer than the width of the window allows, it will wrap around to the next line. Make sure that double quote marks are placed around the quote, and that the author name and year is on a separate line and begins with a dash. The "view" section should also display the number of likes and loves that the quote has received, e.g. This quote has recelved 3 likes and 2 loves. In either format, if the year is unknown (i.e. the "year" key of the quote dictionary is absent), omit the comma and year so that only the author name is shown. Functions in "admin.py" The requirements above mentioned 3 functions - "input_int () ". input_something ( )", and "save_data ()". As part of "admin.py", you must define and use these functions. 1. The "input int () function takes 1 parameter named prompt - a string containing the message to display before waiting for input. The function should repeatedly re-prompt the user (using the prompt parameter) for input until they enter an integer that is greater than or equal to 1. It should then return the value as an integer. - See Workshop 4 for a tosk involving the creation of a very similar function. 2. The "input something () "function takes 1 parameter named prompt - a string containing the message to display before waiting for input. The function should repeatedly re-prompt the user (using the prompt parameter) for input until they enter a value which consists of at least 1 non-whitespace character (i.e. the input cannot be nothing or consist entirely of spaces, tabs, etc.). It should then return the value as a string. - Use the "strip (I" string method on a string to remove whitespoce from the stort and end. If a string cansists entirely of whitespace, it will hove nothing left ance you strip the whitespace away. - Note that exception handling is not needed in this function. 3. The "save_data () " function takes 1 parameter named data_list. (the data list from the main program). The function should open "data.txt" in write mode, then write the data_1ist parameter to the file in JSON format and close the file. This function does not return anything. - This is the only part of the program that should be writing to the file, and it always overwrites the entire content of the file with the entirety of the current dota. - See Reoding 7.1 for an example of using the " "json" madule. You can specify an additional indent parameter in the "dump (1)" function to format the ISON dato nicely in the text file. The definitions of these functions should be at the start of the program (as they are in the starter file provided), and they should be called where needed in the program. Revise Module 4 if you are uncertain about defining and using functions. Ensure that the functions behave exactly as specified; It is important to adhere to the stated function specifications when working on a programming project. In particular, remember that the "prompt" parameter of the input functions is for the text that you want to show as a prompt. Here is an example of the function being called and its output: \[ a g e=\text { input_int ('Enter your age: ') } \Rightarrow \text { Enter your age: I } \] You are welcome to define and use additional functions if you feel they improve your program, but be sure to consider the characteristics and ideals of functions as outlined in Lecture 4. Optional Additions and Enhancements for "admin.py" Below are some suggestions for minor additions and enhancements that you can make to the program to further test and demonstrate your programming ability. They are not required and you can earn full marks in the assignment without implementing them. - When searching, if the search term is not found in any of the quotes/author names, show a "No results found" message. - Add a new option to the program's main menu - "[r]andom". When this option is chosen, the program should display a randomly selected quote from the data list. Use the "full" format (detailed above) when displaying the randomly selected quote. Create a function (or multiple functions) to print appropriately formatted quotes, to minimise repeated code in the program and make it easier to adjust how quotes are displayed. You will need to determine what parameters the function(s) need to receive. - When viewing a quote, enhance the information regarding likes and loves in these ways: - Show a "This quote has not been rated" message if the quote's "Ilkes" and "loves" keys are a. - Pluralise the phrasing correctly, l.e. Use "like" and "love" (not "Hkes" / "loves") for values of 1. - Calculate and display on "Overall Rating using the formula "Hikes + (loves * 2) ". - Add a new option to the program's main menu - "[i]nsert". When this option is chosen, the program should prompt the user to enter all of the details of a quote at once, e.g. "Brevity is the soul of wit." - William Shakespeare, 1602 (the comma and year after the nome con amitted be if it is unknown) Break the string apart using the quote marks, dash and comma to create the dictionary needed to add to the data list, with or without the "year" key depending on whether it was omitted. - Try to make your code robust enough to work correctly even if the quote text contoins characters such as dashes and commas or if there are extra/missing spaces. - This will invalve some relatively complex string manipulation, using string slicing and string methods such as "find ()", "Ifind (1 " and "split ( )". - Allow users to use the search, view and delete options more efficiently by allowing input of "s ", " v ". For example, instead of needing to type "s" and then "tzu", the user could type "s tzu", and instead of needing to type " v " then " 2 ", the user could type " v2 ". - This feature takes a reasonable omount of extra thought and effort to implement efficiently. Overview of "quotes.py" "quotes.py" is a program with a Graphical User Interface (GUI), as covered in Module 9. It should be coded in an Object Oriented style, as covered in Module 8. Everything you need to know in order to develap this program is covered in the first 9 modules of the unit. This program should be developed after "admin.py". The entirety of this program can be implemented in under 120 lines af code (although implementing optional additions may result in a longer program). This number is not a limit or a goal - it is simply provided to prompt you to ask your tutor for advice if your program significantly exceeds it. You must use the "tkinter" module and the "tkinter.messagebox" module to create the GUI. This program uses the data from the "data.txt" file. Similar to the admin program, this program should load the data from the file once only - when the program begins. The program displays the first quote and three buttons labelled "Skip", "Like" and "Love", allowing the user to rate the quote. If the user presses the "Like" or "Love" button, the program adds 1 to the corresponding value in the dictionary of the current quote. The updated list of quote data is then saved in the text file, and the next quote is shown. If the user presses the "Skip" button, the next quote is shown without adding anything to the data. Once the final quote has been rated or skipped, the program ends. A messagebox confirming what has happened is shown between each question, as well as at the end: The following pages detail how to implement the program. 2. Try to open the "data.txt" file in read mode and load the JSON data from the file into an attribute named "self. data", and then close the file. - If any exceptions occur (due to the file not existing, or it not contoining valid ssow dato), show on error messagebox with a "Missing/Invalid file" message and call the "destroy (\}" method on the main window to end the program. Include a "retuyn" stotement in the exception handler after destroying the main window to halt the constructor so that the program ends cleanly- 3. Create an attribute named "self. current_quote" to keep track of which quote is currently being displayed in the GUI, and set it to 0 . - This attribute represents an index number in the list of quotes looded from the text file (se.1F. data). 4. Use Label, Button and Frame widgets from the "tkinter" module to implement the GUI depicted on the previous page. The layout is up to you as long as it functions as specified. - You will save time if you design the GUl and determine exoctly which widgets you will need and how to lay them out before you start witing the code. - See Reading 9.1 for information regarding varlous settings that can be applied to widgets to make them appeor with the desired podding, colour, size, etc. - Do not set the text for the label(s) that will contain the quote/author name/year at this point. They will be set in the "show_quote ( ) method (detalled below). - All three of the buttons call the "rate_quote () "method when clicked, but each need to poss it a different parameter walue - a string of "skip", "likes" or "loves" (to know which button was clicked). This involves setting the "command" of buttons in a special way. Here is some sample code: 5. Lastly, the constructor should end by calling the "show quote ()" method to display the first quote in the GUI, and then call "tkinter. mainloop () " to start the main loop. * To call a method of the closs, include "self. "ot the stort, e.g. "self. show guate ()". That is all that the constructor requires. The following pages detail the other methods required, and some optional additions. You are not required to submit pseudocode for "quotes.py". Optional Additions and Enhancements for "quotes.py" Below are some suggestions for minor additions and enhancements that you can make to the program to further test and demonstrate your programming ability. They are not required and you can earn full marks in the assignment without implementing them. - After loading the data from the text file in the constructor, check if the self. data list is empty and end the program if so - there is no point continuing if there are no quotes to rate! - Show the current quote's likes and loves somewhere in the GUI - either in the main window when displaying the quote, or in the messagebox that appears after skipping/rating the quote. - Alternatively/Additionally, show a "New quote" message if the current quote has no ratings. - Show "Quote X of Y " (e.g. "Quote 1 of 4 ") somewhere in the GUI, to give the user an idea of their position in the list of quotes. - Make the GUI more visually interesting by showing the quote text and the author/year in slightly different ways - e.g. a different font size or style, as pictured in the screenshot above. - Rather than always going through the list of quotes in the order that they are stored in the self. data list, go through them in a random order that never repeats the same quote. The program should still end after all of the quotes have been rated, and correctly record ratings. Methods in the GUI class of "quotes.py" This program requires you to define 2 methods to implement the functionality specified "show_quote ()" and "rate_quote (). These should be defined as part of the GUI class. 1. The "show_quote (s " method is responsible for displaying the details of the current quote in the GUI. It is called at the end of the constructor, and by the "rate quote () " method. It uses self.current_quote to refer to an item in the self. data list. - self. data is a list of dictionaries. Referring to a specific index number of the list will abtain the dictionary of that quote. See Page 3 for detalls regarding the keys of a quote dictionary. - Only include the comma after the author's name and year of the quote if it is known (l.e. if the "year" key exists in the dictionory of the current quote). - The "configure ()" method (see Reoding 9.1) can be used on a widget to change the text it displays. 2. The "rate_quote ()" method is called when the user clicks the "Skip", "Like" or "Love" button. If "Like" or "Love" are clicked, add 1 to the corresponding key ("likes" or "loves") of the current quote's dictionary, then open "data.txt", write the self. data list to it in JSON format, and close the file. If "Skip" is clicked, do not make any change to the data. Show an appropriate messagebox as per the screenshots above. Then, if the current quote is the final quote in the self. data list, show an appropriate messagebox and end the program. Otherwise, add 1 to self. current_quote before calling the "show quote ()" method to display the next quote. - This method recelves a parameter named "rating" that contains a string of "skip", "Tikes" or "loves". If the string is not "skilp", the value exactiy matches the name of the appropriate dictionary key. This can be used to efficiently and elegantly add 1 to the appropriate dictionary key. - Writing dota to the fille will involve very similar code to the "save_da ta U" function of admin.py. - Call the "destroy () "methad on the moin window to end the program if it is the last quote. These methods are all that are required to implement the functionality of the program, but you may write/use additional methods if you feel they improve your program. GUls and OOP are massive topics that we do not explore in depth in the unit. As such, this program is likely to involve more research, experimentation and combining/adapting of examples from the unit content than the main program. This is expected, and much of what this program tests is your ability to rapidly produce a working product despite having limited familiarity or experience. Do not be alarmed/concerned if you find this program confusing or difficult for those reasons, and be sure to check the discussions on the unit site regularly for tips and examples. The GUI program is worth fewer marks than the main program of this assignment

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions