simeonshopov

SoftUni bar income

Feb 26th, 2020
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import re
  2.  
  3. customers = []
  4.  
  5.  
  6. def check_name(i: str):
  7.     search = r'%(?P<name>[A-Z][a-z]+)%'
  8.     match = re.search(search, i)
  9.     if match is None:
  10.         return None
  11.     else:
  12.         return match.group('name')
  13.  
  14.  
  15. def check_product(i: str):
  16.     search = r'<(?P<item>[\w]+)>'
  17.     match = re.search(search, i)
  18.     if match is None:
  19.         return None
  20.     else:
  21.         return match.group('item')
  22.  
  23.  
  24. def check_quantity(i: str):
  25.     search = r'\|(?P<number>[0-9]+)\|'
  26.     match = re.search(search, i)
  27.     if match is None:
  28.         return None
  29.     else:
  30.         return int(match.group('number'))
  31.  
  32.  
  33. def check_price(i: str):
  34.     search = r'((?P<price>[0-9]+(\.[0-9]+)*)\$)'
  35.     match = re.search(search, i)
  36.     if match is None:
  37.         return None
  38.     else:
  39.         return float(match.group('price'))
  40.  
  41.  
  42. while True:
  43.     info = input()
  44.     if info == 'end of shift':
  45.         break
  46.     name = check_name(info)
  47.     product = check_product(info)
  48.     quantity = check_quantity(info)
  49.     price = check_price(info)
  50.     valid = name and product and quantity and price
  51.     if valid:
  52.         customers.append([name, product, quantity * price])
  53.  
  54. total_sum = sum([x[2] for x in customers])
  55.  
  56. # Тук ако ползвам речници няма да стане
  57. # Трябва да принтирам всеки валиден вход дори и да се повтаря, а с речници принтирам само уникални.
  58. [print(f'{x[0]}: {x[1]} - {x[2]:.2f}') for x in customers]
  59. print(f'Total income: {total_sum:.2f}')
Advertisement
Add Comment
Please, Sign In to add comment