csp5110 1 answer below »

In the following information, numbered points describe a requirement of the program, and bullet points (in italics) are additional details, notes and hints regarding the requirement. Ask your tutor if 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 data in the file should be in JSON format, so you will need to use the “load()” function from the “json” module to read the data into your program. See the previous page for details of the data.

§ If any exceptions occur (e.g. due to the file not existing, or it not containing valid JSON data), then simply set the data 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 is the first and only time that the program should need to read anything from the file. After this point, the program uses the data list, 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, [l]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” to respond appropriately (detailed below).

3. If the user enters “a” (add), prompt them to enter all 7 details of a fast food menu item, beginning with the name. Place the details into a new dictionary with the structure shown on the previous page, and append the dictionary to the data list. Finally, write the list to the text file in JSON format to save the data.

§ Use your “inputSomething()” function (detailed below) to prompt the user for the name of the menu item, to ensure that the user is re-prompted until they enter something.

§ Use your “inputInt()” function (detailed below) to prompt the user for the nutritional details of the menu item, to ensure that the user is re-prompted until they enter an integer.

§ Once the new dictionary has been added to the data list, use your “saveChanges()” function (detailed below) to write the changes to the text file. 

4. If the user enters “l” (list), display the names of all menu items in the data list preceded by their index number, or a “No menu items saved” error message if there is nothing in the list.

§ Use a “for” loop to iterate through the items in the data list.

§ You can use the “enumerate()” function to make sure you have a variable containing the index number of each menu item as you loop through them (see Lecture 3).



Tutor picture
Yaman answered

578 answers so far

3 Ratings, (9 Votes)

Related Questions

Recent Questions in Programming Languages

Submit Your Questions Here !
Copy and paste your question here...
Attach Files