Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. # functions go here
  2.  
  3.  
  4. def unit_checker():
  5. unit = not_blank("What is the unit of measurement for these items?: ",
  6. "",
  7. "no")
  8. unit_tocheck = unit.lower()
  9.  
  10. # Abbreviations lists
  11. gram = ["grams", "g", "gms", "gm", "gram"]
  12. kilogram = ["kilograms", "kg", "kgs", "kilogram"]
  13. litre = ["litre", "ltr","liter", "l", "litres","ltrs", "liters"]
  14. millilitre = ["ml", "millilitre", "milliliter", "mls", "millilitres", "milliliters"]
  15. valid = False
  16. while not valid:
  17. if unit_tocheck in gram:
  18. return "gram"
  19. elif unit_tocheck in kilogram:
  20. return "kilogram"
  21. elif unit_tocheck in litre:
  22. return "litre"
  23. elif unit_tocheck in millilitre:
  24. return "millilitre"
  25. else:
  26. global not_right_unit
  27. not_right_unit = "False"
  28. break
  29.  
  30.  
  31. # checks input is a number
  32. def num_check(question):
  33.  
  34. error = "Please enter a number."
  35.  
  36. valid = False
  37. while not valid:
  38. try:
  39. response = float(input(question))
  40. return response
  41. except ValueError:
  42. print(error)
  43.  
  44.  
  45. # Not blank function
  46. def not_blank(question, error_msg, num_ok):
  47. error = error_msg
  48.  
  49. valid = False
  50. while not valid:
  51. response = input(question)
  52. has_errors = ""
  53.  
  54. if num_ok != "yes":
  55. # look at each character in string and if it's a number, complain
  56. for letter in response:
  57. if letter.isdigit() == True:
  58. has_errors = "yes"
  59. error = "Please try again - numbers are not allowed in this field"
  60. break
  61.  
  62. if response == "" or response == " ":
  63. error = "Please type something (this can't be left blank)"
  64. print(error)
  65. continue
  66. elif has_errors != "":
  67. print(error)
  68. continue
  69. else:
  70. return response
  71.  
  72.  
  73. # Main routine...
  74.  
  75. # setting up conversion dictionary
  76. unit_central = {
  77. "litre": 1000,
  78. "millilitre": 1,
  79. "gram": 1,
  80. "kilogram": 1000
  81. }
  82.  
  83. # Set up empty items list
  84. items = []
  85. is_unit_same = []
  86. avg_unit_price = []
  87.  
  88. # just declaring some locally assigned variables
  89. unit = ""
  90. item_weight = ""
  91. item_cost = ""
  92.  
  93. # loop to ask users to enter an item
  94.  
  95. stop = ""
  96. while stop != "xxx":
  97. item = []
  98. user_keeps = ""
  99. # Ask user for item (via not blank function)
  100. item_name = not_blank("Please type in the item name (type 'xxx' to stop): ",
  101. "",
  102. "yes")
  103. # Stop looping if exit code is typed and there are more
  104. # than 2 items
  105. if item_name.lower() == "xxx" and len(items) > 1:
  106. break
  107.  
  108. elif item_name.lower() == "xxx" and len(items) < 2:
  109. print("You need at least two items in the list. "
  110. "Please add more items")
  111. continue
  112. is_item_weight_too_small = ""
  113. while is_item_weight_too_small == "":
  114. item_weight = num_check("Please type in the item weight/volume: ")
  115. # item weight needs to be greater than 0.1 because users might be comparing lutetium
  116. # with other elements and I want them to be able to do that
  117. if item_weight < 0.1:
  118. print("Please enter a number that is more than (or equal to) 0.1.")
  119. continue
  120. else:
  121. break
  122.  
  123. # unit should only be gms, kgs, mls and litres
  124. is_unit_false = ""
  125. while is_unit_false == "":
  126. not_right_unit = ""
  127. unit = unit_checker()
  128. if not_right_unit == "False":
  129. print("Only Gms, Kgs, Mls and Litres are allowed")
  130. else:
  131. break
  132. # check if item cost is below $10
  133. is_item_cost_too_small = ""
  134. while is_item_cost_too_small == "":
  135. item_cost = num_check("Please type in the item cost: $")
  136. if item_cost < 0.10:
  137. print("Please enter a number that is more than or equal to 0.1.")
  138. continue
  139. else:
  140. break
  141.  
  142. print()
  143.  
  144. # add item to list
  145. item.append(item_name)
  146. item.append(item_weight)
  147.  
  148. # turning everything to gram or mls
  149. make_all_units_one = False
  150. while not make_all_units_one:
  151. if unit in unit_central:
  152. multiply_by = unit_central.get(unit)
  153. item[1] = item[1] * multiply_by
  154. break
  155.  
  156. # display unit as grams or millilitres
  157. if unit.startswith("k", 0, 1):
  158. unit = unit.replace(unit, "grams")
  159. elif unit.startswith("l", 0, 1):
  160. unit = unit.replace(unit, "millilitres")
  161.  
  162. # add to list
  163. item.append(item_cost)
  164. item.append(item[2] / item[1]) # find cost per gram and add to item
  165.  
  166. item.append(unit)
  167. items.append(item)
  168.  
  169. print()
  170.  
  171. # sorts by least expensive to most expensive
  172. items.sort(key=lambda x: x[3],)
  173. print("**** Items by Cost <Least Expensive to Most Expensive> ****")
  174. # Every item in items gets gets formatted and printed.
  175. for x in range(len(items)):
  176. print("{} {:.0f} {} costs ${:.4f} per {}".format(items[x][0], items[x][1], items[x][4], items[x][3], items[x][4].replace('s', ''))) # take away the 's' e.g cost per grams should be cost per gram.
  177. avg_unit_price.append(items[x][3])
  178. is_unit_same.append(items[x][4].replace('s', ''))
  179.  
  180. is_unit_same = list(dict.fromkeys(is_unit_same))
  181.  
  182. # if the measuring items are not of the same unit
  183. if len(is_unit_same) != 1:
  184. print()
  185. print("Note: to get accurate results please compare items with the same unit")
  186.  
  187. # Calculate Average Unit Price
  188. avg_unit_price = sum(avg_unit_price) / len(avg_unit_price)
  189.  
  190. print()
  191. # Print Avg Unit Price
  192. print("The average unit price is ${:.4f} in {}s".format(avg_unit_price, items[0][4].replace('s', '')))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement