Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Python Programming: how to write a program that allows user to add and remove items on a grocery list.?
def title():
print('Grocery List Program')
print('--------------------')
print()
print('At each > prompt, type +item_name to add an item to the'+
' grocery list, or')
print('type -item_name to remove an item from the list.' +
' Type DONE when finished.')
print()
def prompt_user():
item_prompt = input('> ') # get food items from user
item_prompt = item_prompt.strip()
return item_prompt.lower()
def obtain_list(item_prompt):
grocery_list = [] # create empty list
item_prompt = item_prompt.strip() # strips lead and trail spaces
while item_prompt != 'done': # control variable for loop
if item_prompt.startswith('+'):
grocery_list.append(item_prompt[1:])# appends item to list
print(item_prompt[1:].strip(), 'added to the list')
elif item_prompt.startswith('-'):
grocery_list.remove(item_prompt[1:])# removes item from list
print(item_prompt[1:].strip(), 'removed from the list')
else:
print('entry must begin with + or -, or type DONE when finished')
item_prompt = prompt_user()
for item_prompt in grocery_list:
print(item_prompt.strip())
def main():
title()
item_prompt = prompt_user()
obtain_list(item_prompt)
main()
this is the code i have so far but i cannot figure out how to print out a message saying that item was already added to the list or that item is not on the list.
example:
>+eggs
eggs added to the list
>+eggs
eggs is already on the list
>-bacon
bacon was not on the list
3 Answers
- 9 years agoFavorite Answer
This is all in Python 3.2.2, but here's some code I came up with that adds, removes, and displays items in a list: (be sure to indent correctly if you copy/paste)
groceryList = []
def mainMenu(): #displays the main menu...
print("\n--Menu--\n1.Add Item\n2.Remove Item\n3.See List\n4.Quit")
choice = int(input("> "))
return choice
def addItem():
#prompts for an item to add, and if the
#item is not in the list, it adds it
print("What item would you like to add?")
item = input("> ")
if item not in groceryList:
groceryList.append(item)
elif item in groceryList:
input("That item is already in your list.\n")
def removeItem():
#this is the same piece of code from below, in 'menuChoice == 3'
#but it has a delete function added in at the end
print("Your grocery list contains:")
i = 0
while i in range(len(groceryList)):
print(groceryList[i])
i = i + 1
print("What would you like to remove?")
item = input("> ")
if item not in groceryList:
input("That doesn't seem to be a part of your list.")
elif item in groceryList:
itemIndex = groceryList.index(item)
del groceryList[itemIndex]
menuChoice = mainMenu()
while menuChoice not in [1,2,3,4]:
#if you enter a number that isn't an option
#it returns you to mainMenu
print("invalid entry")
menuChoice = mainMenu()
while menuChoice in [1,2,3,4]: #I'm sure you get the idea....
if menuChoice == 1:
addItem()
menuChoice = mainMenu()
elif menuChoice == 2:
removeItem()
menuChoice = mainMenu()
elif menuChoice == 3:
#the following stuff just displays the list in a
#nice format, sort of like a list would be naturally written
i = 0
print("Your grocery list contains:")
while i in range(len(groceryList)):
print(groceryList[i])
i = i + 1
input("press enter...")
menuChoice = mainMenu()
else:
break
Source(s): I wrote this program. - 9 years ago
Hope this will help you.