Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_yes(prompt: str = "Yes or No? ", default: bool | None = None) -> bool:
- """ prompt user and only accept answer that is in AFFIRMATION or REJECTION sets
- and return True or False accordingly; return alone can be accepted if default
- set to either True or False in which case the default is returned"""
- while True:
- response = input(prompt).strip().casefold()
- if not response and default is not None:
- return default
- if response in AFFIRMATION:
- return True
- if response in REJECTION:
- return False
- print('Sorry, I did not understand that')
- def menu_choice(menu_name, menu_options):
- print()
- print(menu_name)
- for key, (details, _) in menu_options.items():
- print(key, details)
- while True:
- option = input('Enter option: ')
- if option in menu_options:
- return option, menu_options[option][1]
- print('Sorry, that option is not available, please select again')
- def register():
- print('registering')
- fini = False
- while not fini:
- option, action = menu_choice('shop', menus['shop'])
- if action is None:
- fini = True
- else:
- action()
- def payment():
- print('paying')
- print('Due:', round(order['total']/100, 2))
- # add code to process payment
- order['paid'] = True # assume paid for now
- def close_out():
- print('closing out')
- if order['basket']:
- payment()
- else:
- print('Nothing in basket')
- def add_discount_card():
- if order['discount'] is not None:
- print(f'You have already selected discount {order["discount"]}.')
- if not is_yes('Do you want to replace it? '):
- return
- order['discount'] = menu_choice('discount', menus['discount'])
- def add_new_item():
- items_added = []
- while True:
- item = input('adding new item (just enter to finish): ').strip().lower()
- if not item:
- break
- if item not in items:
- print('Sorry, do not recognise that item')
- continue
- qty = input('How many? ').strip()
- if qty.isdecimal():
- items_added.append([item, int(qty)])
- else:
- print('Sorry, do not understand that')
- if items_added:
- order['basket'].extend(items_added)
- def total_and_pay():
- print('total and paying')
- if order['paid']:
- print('Already paid')
- return
- total = 0
- discount = discounts.get(order['discount'], 0)
- for item, qty in order['basket']:
- total += items[item] * qty
- order['total'] = total
- if total > 0:
- total = total - total * discount/100
- payment()
- def abandon_basket():
- order['basket'] = None
- def basket():
- print('basket')
- for item, qty in order['basket']:
- unit = items[item]
- price = unit * qty
- print(f'{item:10} x {qty} @ {unit:3}p = £{price/100:3.2f}') # could show total price
- AFFIRMATION: frozenset[str] = frozenset(('y', 'yes', 'yup', 'yeh', 'ok', '1'))
- REJECTION = frozenset(('n', 'no', 'nah', 'nope', '0'))
- items = {'apple': 20, 'pear': 25, 'banana': 20, 'orange': 32}
- menus = {
- 'main':
- {
- '1': ('start registering your grocery', register),
- '2': ('close the self-check in register', None),
- '3': ('show basked', basket),
- '4': ('Abandon basket', abandon_basket)
- },
- 'shop': {
- '1': ('to add a discount card', add_discount_card),
- '2': ('to register a new item', add_new_item),
- '3': ('show basked', basket),
- '4': ('to compute the total and pay', total_and_pay),
- '5': ('quit', None)
- },
- 'discount': {
- '1': ('Pensioner Card', 'pension'),
- '2': ('Weekend Card', 'weekend'),
- '3': ('Green Card', 'green')
- }
- }
- discounts = {'pension': 10, 'weekend': 5, 'green': 7}
- order = {'discount': None, 'basket': [], 'paid': False, 'total': 0}
- fini = False
- while not fini:
- option, action = menu_choice('main', menus['main'])
- if action is None:
- if order['paid'] or not order['basket']:
- fini = True
- else:
- print('Sorry, you need to pay first (or abandon basket)')
- else:
- action()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement