Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import sys tems=[] items.append([user_input, user_input]) lef terminate(): if is_exit_confirmed( ) : print(' >>> Bye!') sys.exit() else: print('>>> What can I do for you? ) def
import sys tems=[] items.append([user_input, user_input]) lef terminate(): if is_exit_confirmed( ) : print(' >>> Bye!') sys.exit() else: print('>>> What can I do for you? ) def read_command(): name = input () return name lef print_greeting( ): print('>> Hello, my name is Monty') print (> What can I do for you? ) else: raise Exception('Command not recognized > What can I do for def main(): print_greeting() while True: try: command = read_command() execute_command (command) except Exception as e: print('>>> SoRRY, I could not perform that command. Problem: ', e) Level 7 Save Tasks to Disk Enhance the Monty Level 6 code in the following ways: - Monty saves tasks into a csv file, and loads data from the same file at the start. - Add a delete command that can delete a task at a specific index. A sample output is given below. Note the following: - Monty is able to show at the very start the three tasks loaded from the file. - When item 2 is deleted, the item previously at index 3 moves to position 2. Q) here are some tips: - The filename can be specified in the code. e.g., 1 DATA_FILE = 'monty7.csv' - The following statement will create an empty data.csv file if the file doesn't exist, but will keep the file as it is if it already exists (it simply opens the file in append mode -- which creates the file it if it doesn't exist -- and close it right after). 1 open('data.csv', 'a').close() - The format of the file is up to you. Here is an example: 1 borrow book, done 2 read book, pending return book, pending - The program can load the tasks from the file at the beginning. It can save the data after each command. For example, as follows: 1 items =[] \begin{tabular}{|r|c} 6 & create_file_if_missing(DATA_FILE) \\ 7 & load_data(DATA_FILE) \# load task data from the file \\ 8 & print_greeting() \\ 9 & while True: \\ 10 & try: \\ 11 & command = read_command() \\ 12 & execute_command(command) \\ 13 & save_data(DATA_FILE, items) \# save all tasks in the file \\ 14 & except Exception as e: \\ 15 & print('>>>SORRY, I could not perform that command. Problem: \\ 16 & \\ 17 & main() \end{tabular} G Partial solution Here is the code for loading data from the file. def load_data(filename) : data_file = open (filename) deliveries_reader = csv. read if not row: continue add_item_from_csv_line(row) def add_item_from_csv_line(values): status = True if values[1] == 'done' else False items.append([values[0], status ] ) \# items is a global variable Note that status = True if values [1]== 'done' else False is a shortcut syntax. It is equivalent to the code below: 1 if values [1]== 'done' : status = True else: status = False
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started