Home > Software design >  Basic shopping cart with empty list in Python
Basic shopping cart with empty list in Python

Time:04-07

I can't seem to get this basic code working? I'm tryin to allow people to choose from the given items in available list, check if userInput == available_items, append them to my empty list & allow for users to continue, quit and print their items. Any help is appreciated!

available_items = ['Yogurt', 'Hummus', 'Bananas', 'Stawberries', 'Crackers', 'Veggie Pack', 'Shaving Cream', 'Razors', 'Deodorant' ]
userItems=[]
shopping_1st = True
while shopping_1st:
    userInput = input("Hello valued guest! Choose from the selection of available items to be added to your cart. ")
    response = input("'y' = Continue shopping?  'n' = continue to cart ")
    if str.lower(response) == 'n':
       shopping_1st = False
    elif str.upper(userInput) != available_items:
       print("Item not available:")
       print("Select from available items:")
    else:
        userItems.append(str.lower(userInput))
    for userInput in userItems:
        print(userInput)

CodePudding user response:

You have multiple issues:

  1. str.upper(userInput) != available_items will try to compare a string with a list, which will always return False.
  2. .upper converts all letters to UPPERCASE. Consider using .title().

Try

...
elif userInput.title() not in available_items:
...

CodePudding user response:

Try the following code:

available_items = ['Yogurt', 'Hummus', 'Bananas', 'Stawberries', 'Crackers', 'Veggie Pack', 'Shaving Cream', 'Razors', 'Deodorant' ]
userItems=[]
shopping_1st = True

while shopping_1st:
    userInput = input("Hello valued guest! Choose from the selection of available items to be added to your cart. ")

    if str.capitalize(userInput) not in available_items:
        print("Item not available:")
        print("Select from available items:")
    else:
        userItems.append(str.lower(userInput))

    response = input("'y' = Continue shopping?  'n' = continue to cart ")
    if str.lower(response) == 'n':
       shopping_1st = False       

for userInput in userItems:
    print(userInput)
  • Related