Skip to content

Instantly share code, notes, and snippets.

@cybnz
Created August 2, 2020 06:08
Show Gist options
  • Save cybnz/736c75c640f80145e5fc61819e446fc0 to your computer and use it in GitHub Desktop.
Save cybnz/736c75c640f80145e5fc61819e446fc0 to your computer and use it in GitHub Desktop.
Basic unit to mL converter (does not cope with abbreviations)
# ask user for amount
# ask user for Unit
# check that unit is in dictionary
# if unit in dictionary, convert to mL
# if no unit is given / unit is unknown, leave as is
unit_central = {
"tsp": 5,
"tbs": 15,
"cup": 250,
"ounce": 28.35,
"pint": 473,
"quart": 946,
"pound": 454
}
amount = eval(input("How much? "))
amount = float(amount)
unit = input("Unit? ")
if unit in unit_central:
mult_by = unit_central.get(unit)
amount = amount * mult_by
print("Amount in mL {}".format(amount))
else:
print("{} is unchanged".format(amount))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment