Question
PYTHON SHOPPING LIST Making a shopping list using python. I have everything down except the emptyList function. When I enter -e to empty the list,
PYTHON SHOPPING LIST
Making a shopping list using python. I have everything down except the emptyList function. When I enter "-e" to empty the list, it returns empty, however the text still says "you have "number other than 0" in your list"
this is the instruction and my current code is down below!
emptyList The final function we need to implement is emptyList. It will not work to set the value of theList to empty square brackets.
def emptyList(): theList = []
Since you cannot set the value of the variable, you will need to manipulate the list object to remove the items within the list.
When the list is emptied, report this using a simple message.
# create a shared list
theList = []
def printMenu():
print('Here are the list of commands:')
print( '-p : Print the List')
print( '-e : Empty the List')
print( '-x : Exit')
print( '-h : Print this command list')
def addToList(item):
theList.append(item)
def getInput():
if len(theList) > 0:
print ('You have', len(theList),'in your list')
else:
print('You have your 0 items in your list.')
item = input('Enter an item or command: ')
item = item.strip(' ')
while item == (''):
print('Shopping list items cannot be blank')
item = input('Enter an item or command: ')
item = item.strip(' ')
return item
def printList():
print("Your shopping list:")
#loop to process the items
for item in theList:
#to print all the items in the list
print("\t\t *\t"+item)
def emptyList():
theList.clear
print('The list is empty')
return len(theList)
def startProgram():
print('Welcome to the XYZ Shopping List Program')
def endProgram():
print('Thank you for using the XYZ Shoppling List Program.')
def main():
startProgram()
printMenu()
item = getInput()
while item != '-x':
if item == '-p':
printList()
elif item == '-h':
printMenu()
elif item == '-e':
emptyList()
else:
addToList(item)
item = getInput()
endProgram()
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