Advertisement
gruntfutuk

justfordemo

Dec 9th, 2023 (edited)
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. def is_yes(prompt: str = "Yes or No? ", default: bool | None = None) -> bool:
  2.     """ prompt user and only accept answer that is in AFFIRMATION or REJECTION sets
  3.    and return True or False accordingly; return alone can be accepted if default
  4.    set to either True or False in which case the default is returned"""
  5.     while True:
  6.         response = input(prompt).strip().casefold()
  7.         if not response and default is not None:
  8.             return default
  9.         if response in AFFIRMATION:
  10.             return True
  11.         if response in REJECTION:
  12.             return False
  13.         print('Sorry, I did not understand that')
  14.  
  15. def menu_choice(menu_name, menu_options):
  16.     print()
  17.     print(menu_name)
  18.     for key, (details, _) in menu_options.items():
  19.         print(key, details)
  20.     while True:
  21.         option = input('Enter option: ')
  22.         if option in menu_options:
  23.             return option, menu_options[option][1]
  24.         print('Sorry, that option is not available, please select again')
  25.  
  26. def register():
  27.     print('registering')
  28.     fini = False
  29.     while not fini:
  30.         option, action =  menu_choice('shop', menus['shop'])
  31.         if action is None:
  32.             fini = True
  33.         else:
  34.             action()
  35.  
  36. def payment():
  37.     print('paying')
  38.     print('Due:', round(order['total']/100, 2))
  39.     # add code  to process payment
  40.     order['paid'] = True  # assume paid for now
  41.  
  42. def close_out():
  43.     print('closing out')
  44.     if order['basket']:
  45.         payment()
  46.     else:
  47.         print('Nothing in basket')
  48.  
  49. def add_discount_card():
  50.     if order['discount'] is not None:
  51.         print(f'You have already selected discount {order["discount"]}.')
  52.         if not is_yes('Do you want to replace  it? '):
  53.             return
  54.     order['discount'] = menu_choice('discount', menus['discount'])
  55.  
  56. def add_new_item():
  57.     items_added  =  []
  58.     while True:
  59.         item  = input('adding new item (just enter to finish): ').strip().lower()
  60.         if not item:
  61.             break
  62.         if item not in items:
  63.             print('Sorry, do not recognise that item')
  64.             continue
  65.         qty = input('How many? ').strip()
  66.         if qty.isdecimal():
  67.             items_added.append([item, int(qty)])
  68.         else:
  69.             print('Sorry, do not understand that')
  70.     if items_added:
  71.         order['basket'].extend(items_added)
  72.  
  73. def total_and_pay():
  74.     print('total and paying')
  75.  
  76.     if order['paid']:
  77.         print('Already paid')
  78.         return
  79.  
  80.     total = 0
  81.     discount = discounts.get(order['discount'], 0)
  82.     for item, qty in order['basket']:
  83.         total += items[item] * qty
  84.     order['total'] = total
  85.     if total >  0:
  86.         total = total - total * discount/100
  87.         payment()
  88.  
  89. def abandon_basket():
  90.     order['basket'] = None
  91.  
  92. def basket():
  93.     print('basket')
  94.     for item, qty in order['basket']:
  95.         unit = items[item]
  96.         price = unit * qty
  97.         print(f'{item:10} x {qty} @ {unit:3}p = £{price/100:3.2f}')  # could show total price
  98.  
  99. AFFIRMATION: frozenset[str] = frozenset(('y', 'yes', 'yup', 'yeh', 'ok', '1'))
  100. REJECTION = frozenset(('n', 'no', 'nah', 'nope', '0'))
  101.  
  102. items = {'apple': 20, 'pear': 25, 'banana': 20, 'orange': 32}
  103.  
  104. menus = {
  105.     'main':
  106.         {
  107.             '1': ('start registering your grocery', register),
  108.             '2': ('close the self-check in register', None),
  109.             '3': ('show basked', basket),
  110.             '4': ('Abandon basket', abandon_basket)
  111.         },
  112.  
  113.     'shop': {
  114.         '1': ('to add a discount card', add_discount_card),
  115.         '2': ('to register a new item', add_new_item),
  116.         '3': ('show basked', basket),
  117.         '4': ('to compute the total and pay', total_and_pay),
  118.         '5': ('quit', None)
  119.         },
  120.  
  121.     'discount': {
  122.         '1': ('Pensioner Card', 'pension'),
  123.         '2': ('Weekend Card', 'weekend'),
  124.         '3': ('Green Card', 'green')
  125.         }
  126.     }
  127.  
  128. discounts = {'pension': 10, 'weekend': 5, 'green': 7}
  129. order = {'discount': None, 'basket': [], 'paid': False, 'total': 0}
  130. fini = False
  131. while not fini:
  132.     option, action = menu_choice('main', menus['main'])
  133.     if action is None:
  134.         if order['paid'] or not order['basket']:
  135.             fini = True
  136.         else:
  137.             print('Sorry, you need to pay first (or abandon basket)')
  138.     else:
  139.         action()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement