Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Author: Anton Sanakoev
- Project: Shopping Cart
- Showing Creativity and Exceeding Requirements
- Line 13: Actions string is generated dynamically based on an object of actions
- Line 21: Menu formatting is enhanced for readability
- Line 47: Printing '-empty-' when the cart is empty
- """
- print('Welcome to the Shopping Cart Program!')
- actions = {
- 1: 'Add item',
- 2: 'View cart',
- 3: 'Remove item',
- 4: 'Compute total',
- 5: 'Quit',
- }
- menu = '\n-[Please select one of the following]-'
- for action in actions.items():
- menu += f'\n[{action[0]}] {action[1]}'
- item_names = []
- item_prices = []
- currency = '$'
- while True:
- print(menu)
- choice = int(input('\nPlease enter an action: '))
- match choice:
- case 1:
- item_name = str(input('\nWhat item would you like to add? ')).capitalize()
- item_names.append(item_name)
- item_price = round(float(input(f"What is the price of '{item_name}'? ")), 2)
- item_prices.append(item_price)
- print(f"'{item_name}' has been added to the cart.")
- case 2:
- print('\nThe contents of the shopping cart are:')
- if len(item_names) == 0:
- print('-empty-')
- else:
- for item_position, item_name in enumerate(item_names):
- print(f'{item_position + 1}. {item_name} - {currency}{item_prices[item_position]:.2f}')
- case 3:
- remove_item_position = int(input('\nWhich item would you like to remove? ')) - 1
- if 0 <= remove_item_position < len(item_names):
- item_names.pop(remove_item_position)
- item_prices.pop(remove_item_position)
- print('Item removed.')
- else:
- print('Sorry, that is not a valid item number.')
- case 4:
- print(f'\nThe total price of the items in the shopping cart is {sum(item_prices):.2f}')
- case 5:
- break
- case _:
- print('\nSorry, that is not a valid action')
- print('\nThank you. Goodbye.')
Advertisement
Add Comment
Please, Sign In to add comment