# Name: # Student Number: # This file is provided to you as a starting point for the "admin.py" program of Assignment 2 # of Programming Principles in Semester 1, XXXXXXXXXXIt aims to give you just...

Programming Assignment dealing with JSON



# Name: # Student Number: # This file is provided to you as a starting point for the "admin.py" program of Assignment 2 # of Programming Principles in Semester 1, 2019. It aims to give you just enough code to help ensure # that your program is well structured. Please use this file as the basis for your assignment work. # You are not required to reference it. # The "pass" command tells Python to "do nothing". It is simply a placeholder to ensure that the starter files run smoothly. # They are not needed in your completed program. Replace them with your own code as you complete the assignment. # Import the json module to allow us to read and write data in JSON format. import json # This function repeatedly prompts for input until an integer is entered. # See Point 1 of the "Functions in admin.py" section of the assignment brief. def inputInt(prompt): while True: value = input(prompt) try:numResponse = int(value) except ValueError: print('Invalid input -Try again.') continue return numResponse pass # This function repeatedly prompts for input until a float is entered. # See Point 2 of the "Functions in admin.py" section of the assignment brief. def inputFloat(prompt): while True: value = input(prompt) try:numResponse = float(value) except ValueError: print('Invalid input -Try again.') continue return numResponse pass # This function repeatedly prompts for input until something other than whitespace is entered. # See Point 3 of the "Functions in admin.py" section of the assignment brief. def inputSomething(prompt): while True: value = input(prompt) try:numResponse = strip(value) except ValueError: print('Invalid input -Try again.') continue return numResponse pass # This function opens "data.txt" in write mode and writes the data to it in JSON format. # See Point 4 of the "Functions in admin.py" section of the assignment brief. def saveData(dataList): f = open('data.txt', 'w') # open the file in write mode json.dump(dataList, f) # encode 'data' to json and write to file f.close() pass # Here is where you attempt to open data.txt and read the data into a "data" variable. # If the file does not exist or does not contain JSON data, set "data" to an empty list instead. # This is the only time that the program should need to read anything from the file. # See Point 1 of the "Requirements of admin.py" section of the assignment brief. with open('data.txt', 'r') as f: data = json.load(f) else: data = [] # Print welcome message, then enter the endless loop which prompts the user for a choice. # See Point 2 of the "Details of admin.py" section of the assignment brief. # The rest is up to you. print('Welcome to the Fruit Test Admin Program.') while True: print('\nChoose [a]dd, [l]ist, [s]earch, [v]iew, [d]elete or [q]uit.') choice = input('> ') if choice == 'a': name = input('Enter name of fruit: ') if inputSomething(name): print('In 100 grams of', name,', how many...') calories = inputFloat('Calories are there?: ') fibre = inputFloat('Grams of fibre are there?: ') sugar = inputFloat('Grams of sugar are there?: ') vitamin_c = inputFloat('Milligrams of Vitamin C are there?: ') fruit = { "name": name, "calories": calories, "fibre": fibre, "sugar": sugar, "vitamin_c": vitamin_c } data.append(fruit) saveData(dataList) else: print('Invalid input -Try again.') pass elif choice == 'l': print('\nfruit:') for i, item in enumerate(data, 1): print(i, '. ' + item, sep='',end='') else: print("No fruit saved") # List the current menu items. # See Point 4 of the "Details of admin.py" section of the assignment brief. pass elif choice == 's': # Search the current menu items. # See Point 5 of the "Details of admin.py" section of the assignment brief. pass elif choice == 'v': # View a menu item. # See Point 6 of the "Details of admin.py" section of the assignment brief. pass elif choice == 'd': # Delete a menu item. # See Point 7 of the "Details of admin.py" section of the assignment brief. pass elif choice == 'q': print("Goodbye!") break # Quit the program. pass else: print('Invalid input -Try again.') pass CSP1150 Assignment 2 Semester 1, 2019 CSP1150 Assignment 2 Page 1 Programming Principles (CSP1150) Assignment 2: Individual programming project (Fruit Test) Assignment Marks: Marked out of 25, (25% of unit) Due Date: 3 June 2019, 9:00AM 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 nutritional information about fruit, and stores the data in a text file. Develop this program before “fruit_test.py”.  “fruit_test.py”, a GUI program that uses the data in the text file to quiz the user about the nutritional information of fruit. Develop this program after “admin.py”. The following pages describe the requirements of both programs in detail. Starter files for both of these 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. Semester 1, 2019 CSP1150 Assignment 2 Page 2 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 “fruit_test.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 assignment 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, and then revise and refine 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 for further information and tips regarding writing good pseudocode. Write a separate section of pseudocode for each function you define in your program so that the
Jun 09, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here