Question: Create a function called display _ as _ list display _ as _ list has three parameters display _ items message counter _ reqd display

Create a function called display_as_list
display_as_list has three parameters
display_items
message
counter_reqd
display_items is mandatory and both message and counter_reqd are optional.
display_items is the list of items that needs to be displayed.
message is what might be displayed before each item is displayed. message is a string and defaults to 'Item'.
The message string must not be modified.
counter_reqd is boolean and defaults to True.
If there are items in the list, the function displays them as required and returns True. See the examples below.
If there are no items in the list, the function informs the user accordingly and returns False. See the example below.
If counter_reqd is True then on each line, display the message, counter and item. If counter_reqd is False then on each line, only display the item, not the message or the counter.
display_as_list prints a blank line before printing the individual elements of the list on separate lines as per the examples below.
Hints.
Use print() to generate the blank line.
The solution is between 9 and 13 lines of code ignoring blank lines, comments and the docstring.
Ensure you test for the expected return type and value.
Not all of the tests that are used are shown.
As per the instructions, only paste the function code into the answer box. Do not include any testing code.
The same applies for all the other questions in this lab.
For example:
Test Result
recipe_instructions =['Toast bread', 'Spread butter', 'Spread marmite']
shopping_message = 'Shopping item'
purchase_message = 'Purchase item'
# Use passing by position.
# Leave message and counter_reqd as their defaults
return_value = display_as_list(recipe_instructions)
# Use passing by name
# Pass value by name for display_items
# Leave message and counter_reqd as their defaults
return_value = display_as_list(display_items=recipe_instructions)
# Use passing by name
# Pass value by name for display_items and message
# counter_reqd as the default
return_value = display_as_list(display_items=recipe_instructions, message=shopping_message)
print(f'The function returned {return_value}')
# Use passing by name
# Pass value by name for display_items, message and counter_reqd (explicit as True)
return_value = display_as_list(display_items=recipe_instructions, message=purchase_message, counter_reqd=True)
print(f'The function returned {return_value}')
# Use passing by name
# Pass value by name for display_items, message and counter_reqd
# No counter displayed
return_value = display_as_list(display_items=recipe_instructions, message=purchase_message, counter_reqd=False)
print(f'The function returned {return_value}')
Item 1: Toast bread
Item 2: Spread butter
Item 3: Spread marmite
Item 1: Toast bread
Item 2: Spread butter
Item 3: Spread marmite
Shopping item 1: Toast bread
Shopping item 2: Spread butter
Shopping item 3: Spread marmite
The function returned True
Purchase item 1: Toast bread
Purchase item 2: Spread butter
Purchase item 3: Spread marmite
The function returned True
Toast bread
Spread butter
Spread marmite
The function returned True
# Test for empty list
# Use passing by name
# Pass value by name for display_items and counter_reqd
# Leave message as default
recipe_instructions =[]
return_value = display_as_list(display_items = recipe_instructions, counter_reqd = True)
instructions =[]
return_value = display_as_list(display_items = instructions, counter_reqd = False)
Sorry, the list is empty.
Sorry, the list is empty.
# Display shopping list options
shopping_options =[
'Shopping list options.',
'A) Add an item.',
'R) Remove an item by its item number.',
'D) Display the total number of items in the list.',
'L) List all the items.',
'S) Sort the list.',
'E) Empty the list.',
'C) Count the instances of an item in the list.',
'Q) Quit.'
]
# Pass by both position and name
return_value = display_as_list(shopping_options, counter_reqd = False)
Shopping list options.
A) Add an item.
R) Remove an item by its item number.
D) Display the total number of items in the list.
L) List all the items.
S) Sort the list.
E) Empty the list.
C) Count the instances of an item in the list.
Q) Quit.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!