Question: Create a function called display _ as _ list display _ as _ list has three parameters display _ items message counter _ reqd display
Create a function called displayaslist
displayaslist has three parameters
displayitems
message
counterreqd
displayitems is mandatory and both message and counterreqd are optional.
displayitems 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.
counterreqd 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 counterreqd is True then on each line, display the message, counter and item. If counterreqd is False then on each line, only display the item, not the message or the counter.
displayaslist 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 and 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
recipeinstructions Toast bread', 'Spread butter', 'Spread marmite'
shoppingmessage 'Shopping item'
purchasemessage 'Purchase item'
# Use passing by position.
# Leave message and counterreqd as their defaults
returnvalue displayaslistrecipeinstructions
# Use passing by name
# Pass value by name for displayitems
# Leave message and counterreqd as their defaults
returnvalue displayaslistdisplayitemsrecipeinstructions
# Use passing by name
# Pass value by name for displayitems and message
# counterreqd as the default
returnvalue displayaslistdisplayitemsrecipeinstructions, messageshoppingmessage
printfThe function returned returnvalue
# Use passing by name
# Pass value by name for displayitems, message and counterreqd explicit as True
returnvalue displayaslistdisplayitemsrecipeinstructions, messagepurchasemessage, counterreqdTrue
printfThe function returned returnvalue
# Use passing by name
# Pass value by name for displayitems, message and counterreqd
# No counter displayed
returnvalue displayaslistdisplayitemsrecipeinstructions, messagepurchasemessage, counterreqdFalse
printfThe function returned returnvalue
Item : Toast bread
Item : Spread butter
Item : Spread marmite
Item : Toast bread
Item : Spread butter
Item : Spread marmite
Shopping item : Toast bread
Shopping item : Spread butter
Shopping item : Spread marmite
The function returned True
Purchase item : Toast bread
Purchase item : Spread butter
Purchase item : 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 displayitems and counterreqd
# Leave message as default
recipeinstructions
returnvalue displayaslistdisplayitems recipeinstructions, counterreqd True
instructions
returnvalue displayaslistdisplayitems instructions, counterreqd False
Sorry, the list is empty.
Sorry, the list is empty.
# Display shopping list options
shoppingoptions
'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
returnvalue displayaslistshoppingoptions, counterreqd 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
