Question
Enhance the Monty Level 3 code to improve the functionality as per the sample output given below. >>> Hello, my name is Monty >>> What
Enhance the Monty Level 3 code to improve the functionality as per the sample output given below.
>>> Hello, my name is Monty >>> What can I do for you? list >>> Nothing to list >>> What can I do for you? add read book >>> What can I do for you? list >>> List of items: 1. read book >>> What can I do for you? add return book >>> What can I do for you? list >>> List of items: 1. read book 2. return book >>> What can I do for you? exit >>> Are you sure? y/n y >>> Bye!
You can use the list slicing syntax to extract a portion of a string.
s = 'abcdefgh' print(s[2:]) print(s[:5]) | cdefgh abcde |
The above technique can be used to extract the item description from an add command.
Partial Solution:
import sys
items = []
def print_items(): if len(items) == 0: print('>>> Nothing to list') else: for i, item in enumerate(items): # ...
def add_item(user_input): # ...
def terminate(): # ...
# ...
def execute_command(command): if command == '': return elif command == 'exit': terminate() elif command == 'list': print_items() elif command.startswith('add '): add_item(command) else: print('>>> OOPS! Unknown command')
def main(): print_greeting() while True: command = read_command() execute_command(command)
main()
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