This site is from a past semester! The current version will be here when the new semester starts.
TE3201 2020
  • Full Timeline
  • Week 1 [Jan 13]
  • Week 2 [Jan 20]
  • Week 3 [Jan 27]
  • Week 4 [Feb 3]
  • Week 5 [Feb 10]
  • Week 6 [Feb 17]
  • Week 7 [Mar 2]
  • Week 8 [Mar 9]
  • Week 9 [Mar 16]
  • Week 10 [Mar 23]
  • Week 11 [Mar 30]
  • Week 12 [Apr 6]
  • Week 13 [Apr 13]
  • SE Textbook
  • Programming Textbook
  • Admin Info
  • Forum
  • Instructors
  • Announcements
  • repl.it link
  • Files (slides, handouts etc.)
  • Project Info
  • Week 5 [Feb 10] - Tasks

    1. Submit weekly exercises
    2. Submit weekly project increment Level 5. Mark Tasks as Done, Level 6. Give Help (optional)

    1 Submit weekly exercises

    • As usual

    2 Submit weekly project increments

    Programming   Level 5. Mark Tasks as Done

    Level 5 Mark Tasks as Done

    Enhance the Monty Level 4 code in the following ways:

    • Add a done command so that the user can mark a task as done. e.g., done 2 marks the task at index 2 as 'done'.
    • Show appropriate error messages if the user gives an invalid index for the done command

    A sample output is given below.

    >>> Hello, my name is Monty
    >>> What can I do for you?

    add borrow book
    >>> What can I do for you?

    add read book
    >>> What can I do for you?

    add return book
    >>> What can I do for you?

    list
    >>> List of items:
    [✗] 1. borrow book
    [✗] 2. read book
    [✗] 3. return book
    >>> What can I do for you?

    done 1
    >>> What can I do for you?

    list
    >>> List of items:
    [✓] 1. borrow book
    [✗] 2. read book
    [✗] 3. return book
    >>> What can I do for you?

    done abc
    >>> SORRY, I could not perform that command. Problem: abc is not a number
    >>> What can I do for you?

    done 5
    >>> SORRY, I could not perform that command. Problem: No item at index 5
    >>> What can I do for you?

    done 0
    >>> SORRY, I could not perform that command. Problem: Index must be greater than 0
    >>> What can I do for you?

    garbage
    >>> SORRY, I could not perform that command. Problem: Command not recognized
    >>> What can I do for you?

    exit
    >>> Are you sure? y/n
    y
    >>> Bye!

    Each task has two data values: the description and the 'done' status. You can use a list to hold these two data items. That means your list of tasks will be a list containing lists. Example:

    tasks = []
    tasks.append(['read book', False])
    print('Description of the first task:', tasks[0][0])

    if tasks[0][1]:
    print('✓')
    else:
    print('✗')

    You can use exceptions to identify and handle errors in the command.

    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)

    items = []

    # ...

    def get_item_for_index(index_as_string):
    try:
    index = int(index_as_string.strip())
    except Exception:
    raise ValueError(index_as_string + ' is not a number ')

    if index < 1:
    raise ValueError('Index must be greater than 0 ')

    try:
    return items[index - 1]
    except IndexError:
    raise ValueError('No item at index ' + index_as_string)


    def mark_item_as_done(user_input):
    index_as_string = user_input[5:]
    get_item_for_index(index_as_string)[1] = True


    def execute_command(command):
    if command == '':
    return
    elif command == 'exit':
    terminate()
    elif command == 'list':
    print_items()
    elif command.startswith('add '):
    add_item(command)
    elif command.startswith('done '):
    mark_item_as_done(command)
    else:
    raise Exception('Command not recognized')


    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)


    main()
    Programming   Level 6. Give Help optional

    Level 6 Give Help

    Enhance the Monty Level 5 code in the following ways:

    • Add a help command so that the user can view how to use the app.
    • Improve the formatting of the text displayed to the user to make the user experience nicer.

    A sample output is given below.

    *******************************************************************************************
    * __ __ _ _ __ __ _ *
    * \ \ / / | | | | | \/ | | | *
    * \ \ /\ / /__| | ___ ___ _ __ ___ ___ | |_ ___ | \ / | ___ _ __ | |_ _ _ *
    * \ \/ \/ / _ \ |/ __/ _ \| '_ ' _ \ / _ \ | __/ _ \ | |\/| |/ _ \| '_ \| __| | | | *
    * \ /\ / __/ | (_| (_) | | | | | | __/ | || (_) | | | | | (_) | | | | |_| |_| | *
    * \/ \/ \___|_|\___\___/|_| |_| |_|\___| \__\___/ |_| |_|\___/|_| |_|\__|\__, | *
    * __/ | *
    * |___/ *
    *******************************************************************************************

    >>> What can I do for you?

    help
    >>> I'm glad you asked. Here it is:
    ==================================================
    Monty can understand the following commands:

    add DESCRIPTION
    Adds a task to the list
    Example: add read book
    done INDEX
    Marks the task at INDEX as 'done'
    Example: done 1
    exit
    Exits the application
    help
    Shows the help information
    list
    Lists the tasks in the list
    --------------------------------------------------

    >>> What can I do for you?

    add read book
    >>> Task added to the list
    >>> What can I do for you?

    add return book
    >>> Task added to the list
    >>> What can I do for you?

    done 1
    >>> Congrats on completing a task! :-)
    >>> What can I do for you?

    list
    >>> Here is the list of tasks:
    ==================================================
    STATUS | INDEX | DESCRIPTION
    --------------------------------------------------
    ✓ | 1 | read book
    ✗ | 2 | return book
    --------------------------------------------------
    >>> What can I do for you?

    You can use triple quotes to define a long string such as the help text.

    help_text ='''
    long text
    more text
    '''

    You can generate ASCII art using online resources such as http://patorjk.com/software/taag

    def print_greeting():
    banner = '''
    *******************************************************************************************
    * __ __ _ _ __ __ _ *
    * \ \ / / | | | | | \/ | | | *
    * \ \ /\ / /__| | ___ ___ _ __ ___ ___ | |_ ___ | \ / | ___ _ __ | |_ _ _ *
    * \ \/ \/ / _ \ |/ __/ _ \| '_ ' _ \ / _ \ | __/ _ \ | |\/| |/ _ \| '_ \| __| | | | *
    * \ /\ / __/ | (_| (_) | | | | | | __/ | || (_) | | | | | (_) | | | | |_| |_| | *
    * \/ \/ \___|_|\___\___/|_| |_| |_|\___| \__\___/ |_| |_|\___/|_| |_|\__|\__, | *
    * __/ | *
    * |___/ *
    *******************************************************************************************
    '''
    print(banner.strip(), '\n')