Skip to content

Instantly share code, notes, and snippets.

@cybnz
Created August 2, 2020 05:03
Show Gist options
  • Save cybnz/35df63b4cc2a2e0b9af6743425c36cd8 to your computer and use it in GitHub Desktop.
Save cybnz/35df63b4cc2a2e0b9af6743425c36cd8 to your computer and use it in GitHub Desktop.
Scales ingredients correctly but formatting needs to be fixed
# Ingredients list
# Number Checking Function
def num_check(question):
error = "Please enter a number that is more than zero"
valid = False
while not valid:
response = input(question)
if response.lower() == "x":
return "x"
else:
try:
if float(response) <= 0:
print(error)
else:
return response
except ValueError:
print(error)
# Not blank function goes here
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"
break
if response == "":
print(error)
continue
elif has_errors != "":
print(error)
continue
else:
return response
# Main Routine...
# Replace line below with component 3 in due course...
scale_factor = float(input("Scale Factor: "))
# Set up an empty ingredients list
ingredients = []
# Loop to ask users to enter an ingredient
stop = ""
while stop != "x":
amount = num_check("What is the amount for the ingredient? ")
# Stop looping If exit code is typed and there are more than 2 ingredients...
if amount.lower() == "x" and len(ingredients) > 1:
break
elif amount.lower() == "x" and len(ingredients) < 2:
print("You need at least two ingredients in this list. Please add more ingredients.")
# If exit code is not entered, add ingredients to list
else:
# Ask user for ingredient (via not blank function)
get_ingredients = not_blank("Please type in an ingredient name: ", "This can't be blank", "yes")
amount = float(amount) * scale_factor
ingredients.append("{} units {}".format(amount, get_ingredients))
# Output list
print(ingredients)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment