Advertisement
nein_yards

pisa.py

Dec 28th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. from enum import Enum
  2.  
  3. Size = Enum('Size', 'SMALL MEDIUM LARGE')
  4. Base = Enum('Base', 'THIN THICK')
  5. ExtraTopping = Enum('ExtraTopping', 'PEPPERONI CHICKEN EXTRA_CHEESE MUSHROOMS SPINACH OLIVES')
  6.  
  7. def yes_or_no(question):
  8. while True:
  9. reply = str(input(question + ' (y/n): ')).lower().strip()
  10. if reply[:1] == 'y': return True
  11. if reply[:1] == 'n': return False
  12.  
  13. def percent_string(fraction):
  14. return str(round(fraction * 100, 1)) + '%'
  15.  
  16. def validated_enum_input(choice_enum, length, alias, is_serial=True):
  17. print('Enter ' + alias)
  18. for i in range(1, length + 1):
  19. print(''.join(['\t', str(i), ': ', choice_enum(i).name]))
  20. while True:
  21. try:
  22. result = Size(int(input('>>> ')))
  23. break
  24. except ValueError:
  25. if is_serial:
  26. print('Please enter a number between 1 and ' + str(length))
  27. else:
  28. print('Please enter a valid number')
  29. return result
  30.  
  31. total_ordered_pizzas = 0
  32. pizza_topping_counts = [0 for i in range(6)]
  33. day_over = False
  34.  
  35. while not day_over:
  36. topping_names = ['TOMATOES', 'CHEESE']
  37. extra_topping_numbers = []
  38. pizza_size = validated_enum_input(Size, 3, 'pizza size')
  39. pizza_base = validated_enum_input(Base, 2, 'pizza base')
  40.  
  41. print('\nDefault toppings: ' + ', '.join(topping_names))
  42. while True:
  43. if len(topping_names) >= 5:
  44. break
  45. if not yes_or_no('Would you like any more toppings?'):
  46. break
  47. while True:
  48. try:
  49. extra_topping_number = validated_enum_input(ExtraTopping, 6, 'pizza topping', False)
  50. topping_name = ExtraTopping(extra_topping_number).name
  51. if topping_name in topping_names: raise ValueError
  52. break
  53. except ValueError:
  54. print('Please enter a valid number')
  55. topping_names.append(topping_name)
  56. extra_topping_numbers.append(extra_topping_number)
  57.  
  58. print(f'\nYour order is a {pizza_size.name} pizza with a {pizza_base.name} base')
  59. print('Your toppings are: ' + ', '.join(topping_names))
  60.  
  61. if yes_or_no('\nWould you like to confirm your order?'):
  62. for extra_topping_number in extra_topping_numbers:
  63. pizza_topping_counts[extra_topping_number - 1] += 1
  64. total_ordered_pizzas += 1
  65. print('Your order number is: ' + f'{total_ordered_pizzas:04d}')
  66. else:
  67. print('Order cancelled')
  68.  
  69. day_over = yes_or_no('\nWould you like to end the day?')
  70.  
  71. print('\nTotal Ordered Pizzas: ' + str(total_ordered_pizzas))
  72.  
  73. total_topping_count = sum(pizza_topping_counts)
  74. if total_topping_count == 0:
  75. print('No extra toppings ordered today')
  76. quit()
  77.  
  78. min_topping_count = min(pizza_topping_counts)
  79. max_topping_count = max(pizza_topping_counts)
  80.  
  81. popular_toppings = []
  82. unpopular_toppings = []
  83.  
  84. for i in range(6):
  85. if pizza_topping_counts[i] == max_topping_count:
  86. popular_toppings.append(ExtraTopping(i + 1).name)
  87.  
  88. if pizza_topping_counts[i] == min_topping_count:
  89. unpopular_toppings.append(ExtraTopping(i + 1).name)
  90.  
  91. print(' - '.join(['Most popular topping' +
  92. ('s' if len(popular_toppings) > 1 else ''),
  93. ', '.join(popular_toppings),
  94. percent_string(max_topping_count / total_topping_count)]))
  95.  
  96. print(' - '.join(['Least popular topping' +
  97. ('s' if len(unpopular_toppings) > 1 else ''),
  98. ', '.join(unpopular_toppings),
  99. percent_string(min_topping_count / total_topping_count)]))
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement