SuperSonicBlast

Shopping Cart

Feb 9th, 2024 (edited)
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. """
  2. Author: Anton Sanakoev
  3. Project: Shopping Cart
  4.  
  5. Showing Creativity and Exceeding Requirements
  6. Line 13: Actions string is generated dynamically based on an object of actions
  7. Line 21: Menu formatting is enhanced for readability
  8. Line 47: Printing '-empty-' when the cart is empty
  9. """
  10.  
  11. print('Welcome to the Shopping Cart Program!')
  12.  
  13. actions = {
  14.     1: 'Add item',
  15.     2: 'View cart',
  16.     3: 'Remove item',
  17.     4: 'Compute total',
  18.     5: 'Quit',
  19. }
  20.  
  21. menu = '\n-[Please select one of the following]-'
  22. for action in actions.items():
  23.     menu += f'\n[{action[0]}] {action[1]}'
  24.  
  25. item_names = []
  26. item_prices = []
  27. currency = '$'
  28.  
  29. while True:
  30.     print(menu)
  31.  
  32.     choice = int(input('\nPlease enter an action: '))
  33.  
  34.     match choice:
  35.         case 1:
  36.             item_name = str(input('\nWhat item would you like to add? ')).capitalize()
  37.             item_names.append(item_name)
  38.  
  39.             item_price = round(float(input(f"What is the price of '{item_name}'? ")), 2)
  40.             item_prices.append(item_price)
  41.  
  42.             print(f"'{item_name}' has been added to the cart.")
  43.         case 2:
  44.             print('\nThe contents of the shopping cart are:')
  45.            
  46.             if len(item_names) == 0:
  47.                 print('-empty-')
  48.             else:
  49.                 for item_position, item_name in enumerate(item_names):
  50.                     print(f'{item_position + 1}. {item_name} - {currency}{item_prices[item_position]:.2f}')
  51.         case 3:
  52.             remove_item_position = int(input('\nWhich item would you like to remove? ')) - 1
  53.  
  54.             if 0 <= remove_item_position < len(item_names):
  55.                 item_names.pop(remove_item_position)
  56.                 item_prices.pop(remove_item_position)
  57.  
  58.                 print('Item removed.')
  59.             else:
  60.                 print('Sorry, that is not a valid item number.')
  61.         case 4:
  62.             print(f'\nThe total price of the items in the shopping cart is {sum(item_prices):.2f}')        
  63.         case 5:
  64.             break
  65.         case _:
  66.             print('\nSorry, that is not a valid action')
  67.  
  68. print('\nThank you. Goodbye.')
Advertisement
Add Comment
Please, Sign In to add comment