Home > Mobile >  Shopping cart Python
Shopping cart Python

Time:04-22

I've noticed that when I remove a item, I need to select the checkout option multiple times. Anybody know why this is happening?

my_cart = []

def customerDecision(addItem, input1):
  while True:
    customerResponse = input("What would you like to do? add / remove / show / checkout / quit ")
    if customerResponse.lower() == "add":
      addItem = input('add what? ')
      my_cart.append(addItem) 
      
    elif customerResponse.lower() == "show":
      print(my_cart)
   
    elif customerResponse.lower() == "remove":
      input1 = input(f'remove what: {my_cart} ')
      print(f'{input1} removed...')

    elif input1 in my_cart:
      my_cart.remove(input1)
      
    elif customerResponse.lower() == "checkout":
      for addItem in my_cart:
        print(f"Thanks for purchasing {my_cart}: '\n'see you next time!")
        break

    elif customerResponse.lower() == "quit":
      print("Thank you! We hope you find what you're looking for next time")
      break

    else:
      print("Please try a valid command.")

customerDecision(addItem='', input1='')

CodePudding user response:

Like this:

    elif customerResponse.lower() == "remove":
      input1 = input(f'remove what: {my_cart} ')
      if input1 in my_cart:
        my_cart.remove(input1)
        print(f'{input1} removed...')

By the way, it would be more efficient if you said

    customerResponse = customerResponse.lower()

once at the top, rather than calling that function 5 times.

  • Related