nein_yards

pz.py

Dec 30th, 2020 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. def validate_input(subject, options, parse_func=lambda x: x):
  2. while True:
  3. try:
  4. x = parse_func(input(f'Enter {subject} ({"/".join(map(str, options))})\n>>> '))
  5. if x not in options: raise ValueError
  6. break
  7. except ValueError:
  8. print('Please enter a valid option')
  9. return x
  10.  
  11. def percent_str(a, b):
  12. return str(round(a / b * 100, 1)) + '%'
  13.  
  14. extra_topping_names = ['pepperoni', 'chicken', 'extracheese', 'mushrooms', 'spinach', 'olives']
  15. topping_counts = [0 for i in range(6)]
  16. total_ordered_pizzas = 0
  17. total_topping_count = 0
  18. while True:
  19. size = validate_input('pizza size', ['small', 'medium', 'large'], str.lower)
  20. base = validate_input('pizza base', ['thin', 'thick'], str.lower)
  21. extra_topping_count = validate_input('number of extra toppings', [0, 1, 2, 3], int)
  22. toppings = ['tomato', 'cheese']
  23. topping_options = extra_topping_names.copy()
  24. print('Default toppings: ' + ', '.join(toppings))
  25. for i in range(extra_topping_count):
  26. extra_topping = validate_input(f'extra topping {str(i+1)}', topping_options, str.lower)
  27. topping_options.remove(extra_topping)
  28. toppings.append(extra_topping)
  29. print(f'\nYour order is a {size} pizza with a {base} base')
  30. print('Your toppings are: ' + ', '.join(toppings))
  31. if input('\nEnter "confirm" to confirm your order: ').lower() == 'confirm':
  32. for i in range(6):
  33. if extra_topping_names[i] in toppings:
  34. topping_counts[i] += 1
  35. total_topping_count += extra_topping_count
  36. total_ordered_pizzas += 1
  37. print('\nYour order number is: ' + f'{total_ordered_pizzas:04d}')
  38. else:
  39. print('Order cancelled')
  40. if input('Enter "end" to end day: ').lower() == 'end': break
  41. if total_ordered_pizzas == 0:
  42. print('No pizzas ordered today')
  43. elif total_ordered_pizzas == 0:
  44. print('No extra toppings ordered today')
  45. else:
  46. max_topping_count = max(topping_counts)
  47. min_topping_count = min(topping_counts)
  48. popular_toppings = [extra_topping_names[i] for i in range(6) if topping_counts[i] == max_topping_count]
  49. unpopular_toppings = [extra_topping_names[i] for i in range(6) if topping_counts[i] == min_topping_count]
  50.  
  51. print(' - '.join(['\nMost popular topping' +
  52. ('s' if len(popular_toppings) > 1 else ''),
  53. ', '.join(popular_toppings),
  54. percent_str(max_topping_count, total_topping_count)]))
  55. print(' - '.join(['Least popular topping' +
  56. ('s' if len(unpopular_toppings) > 1 else ''),
  57. ', '.join(unpopular_toppings),
  58. percent_str(min_topping_count, total_topping_count)]))
  59.  
Add Comment
Please, Sign In to add comment