Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # functions go here
- def unit_checker():
- unit = not_blank("What is the unit of measurement for these items?: ",
- "",
- "no")
- unit_tocheck = unit.lower()
- # Abbreviations lists
- gram = ["grams", "g", "gms", "gm", "gram"]
- kilogram = ["kilograms", "kg", "kgs", "kilogram"]
- litre = ["litre", "ltr","liter", "l", "litres","ltrs", "liters"]
- millilitre = ["ml", "millilitre", "milliliter", "mls", "millilitres", "milliliters"]
- valid = False
- while not valid:
- if unit_tocheck in gram:
- return "gram"
- elif unit_tocheck in kilogram:
- return "kilogram"
- elif unit_tocheck in litre:
- return "litre"
- elif unit_tocheck in millilitre:
- return "millilitre"
- else:
- global not_right_unit
- not_right_unit = "False"
- break
- # checks input is a number
- def num_check(question):
- error = "Please enter a number."
- valid = False
- while not valid:
- try:
- response = float(input(question))
- return response
- except ValueError:
- print(error)
- # Not blank function
- def not_blank(question, error_msg, num_ok):
- error = error_msg
- valid = False
- while not valid:
- response = input(question)
- has_errors = ""
- if num_ok != "yes":
- # look at each character in string and if it's a number, complain
- for letter in response:
- if letter.isdigit() == True:
- has_errors = "yes"
- error = "Please try again - numbers are not allowed in this field"
- break
- if response == "" or response == " ":
- error = "Please type something (this can't be left blank)"
- print(error)
- continue
- elif has_errors != "":
- print(error)
- continue
- else:
- return response
- # Main routine...
- # setting up conversion dictionary
- unit_central = {
- "litre": 1000,
- "millilitre": 1,
- "gram": 1,
- "kilogram": 1000
- }
- # Set up empty items list
- items = []
- avg_unit_price = []
- # just declaring some locally assigned variables
- unit = ""
- item_weight = ""
- item_cost = ""
- # loop to ask users to enter an item
- stop = ""
- while stop != "xxx":
- item = []
- user_keeps = ""
- # Ask user for item (via not blank function)
- item_name = not_blank("Please type in the item name (type 'xxx' to stop): ",
- "",
- "yes")
- # Stop looping if exit code is typed and there are more
- # than 2 items
- if item_name.lower() == "xxx" and len(items) > 1:
- break
- elif item_name.lower() == "xxx" and len(items) < 2:
- print("You need at least two items in the list. "
- "Please add more items")
- continue
- is_item_weight_too_small = ""
- while is_item_weight_too_small == "":
- item_weight = num_check("Please type in the item weight/volume: ")
- # item weight needs to be greater than 0.1 because users might be comparing lutetium
- # with other elements and I want them to be able to do that
- if item_weight < 0.1:
- print("Please enter a number that is more than (or equal to) 0.1.")
- continue
- else:
- break
- # unit should only be gms, kgs, mls and litres
- is_unit_false = ""
- while is_unit_false == "":
- not_right_unit = ""
- unit = unit_checker()
- if not_right_unit == "False":
- print("Only Gms, Kgs, Mls and Litres are allowed")
- else:
- break
- # check if item cost is below $10
- is_item_cost_too_small = ""
- while is_item_cost_too_small == "":
- item_cost = num_check("Please type in the item cost: $")
- if item_cost < 0.10:
- print("Please enter a number that is more than or equal to 0.1.")
- continue
- else:
- break
- print()
- # add item to list
- item.append(item_name)
- item.append(item_weight)
- # turning everything to gram or mls
- make_all_units_one = False
- while not make_all_units_one:
- if unit in unit_central:
- multiply_by = unit_central.get(unit)
- item[1] = item[1] * multiply_by
- break
- # display unit as grams or millilitres
- if unit.startswith("k", 0, 1):
- unit = unit.replace(unit, "grams")
- elif unit.startswith("l", 0, 1):
- unit = unit.replace(unit, "millilitres")
- # add to list
- item.append(item_cost)
- item.append(item[2] / item[1]) # find cost per gram and add to item
- item.append(unit)
- items.append(item)
- print()
- # sorts by least expensive to most expensive
- items.sort(key=lambda x: x[3],)
- print("**** Items by Cost <Least Expensive to Most Expensive> ****")
- # Every item in items gets gets formatted and printed.
- for x in range(len(items)):
- 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.
- avg_unit_price.append(items[x][3])
- # Calculate Average Unit Price
- avg_unit_price = sum(avg_unit_price) / len(avg_unit_price)
- print()
- # Print Avg Unit Price
- 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