Part 2 of 2 - We will use lists, with custom functions that accept parameters and return a list value.
liG = ["apples","milk", "eggs" ]
liG.sort()
# Declare custom functions.
def listG(liMain):
print("\nHere is your grocery list:\n")
intIndex = 0
for strItem in liMain:
print(intIndex, strItem)
intIndex += 1
print("\n")
# Main part of the app
print("%"*60)
print("Grocery List App".center(60))
print("%"*60)
while True:
# Menu
print("\nMain Menu:")
print("1. Add an Item")
print("2. Remove an Item")
print("3. List Items\n")
strMenu = input("Choose Option 1-3: ")
if (strMenu == "1"):
pass
elif (strMenu == "2"):
pass
elif (strMenu == "3"):
# List items in the list
listG(liG)
else:
print("That isn't an option. Please try again.")
strCont = input("Do you want to do anything else? (y/n): ").lower().strip()
if (strCont == "y"):
pass
elif (strCont == "n"):
break
else:
print("That isn't an option.\nReturning to Main Menu.")
print("Thank you for using the Grocery List app!")
liG = ["apples","milk", "eggs" ]
liG.sort()
# Declare custom functions.
def listG(liMain):
print("\nHere is your grocery list:\n")
intIndex = 0
for strItem in liMain:
print(intIndex, strItem)
intIndex += 1
print("\n")
def addItem(liMain):
strItem = input("What would you like to add? ")
liMain.append(strItem)
liMain.sort()
listG(liMain)
return liMain
def removeItem(liMain):
listG(liMain)
while True:
strIndex = input("What is the index of the item to remove? ")
if strIndex.isnumeric():
intIndex = int(strIndex)
strRemoved = liMain.pop(intIndex)
print(f"You have removed {strRemoved} from the list.\n")
break
else:
print("You must enter a number.")
return liMain
# Main part of the app
print("%"*60)
print("Grocery List App".center(60))
print("%"*60)
while True:
# Menu
print("\nMain Menu:")
print("1. Add an Item")
print("2. Remove an Item")
print("3. List Items\n")
strMenu = input("Choose Option 1-3: ")
if (strMenu == "1"):
liG = addItem(liG)
elif (strMenu == "2"):
liG = removeItem(liG)
elif (strMenu == "3"):
# List items in the list
listG(liG)
else:
print("That isn't an option. Please try again.")
strCont = input("Do you want to do anything else? (y/n): ").lower().strip()
if (strCont == "y"):
pass
elif (strCont == "n"):
break
else:
print("That isn't an option.\nReturning to Main Menu.")
print("Thank you for using the Grocery List app!")