Created
August 2, 2020 04:48
-
-
Save cybnz/e5f521cacfe380afcb8ba78542316012 to your computer and use it in GitHub Desktop.
Get ingredient amount and scale it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ingredients list | |
# Number Checking Function | |
def num_check(question): | |
error = "Please enter a number that is more than zero" | |
valid = False | |
while not valid: | |
try: | |
response = float(input(question)) | |
if 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? ") | |
scaled = amount * scale_factor | |
# Ask user for ingredient (via not blank function) | |
get_ingredients = not_blank("Please type in an ingredient name: ", "This can't be blank", "yes") | |
# Stop looping If exit code is typed and there are more than 2 ingredients... | |
if get_ingredients.lower() == "x" and len(ingredients) > 1: | |
break | |
elif get_ingredients.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: | |
ingredients.append(get_ingredients) | |
# Output list | |
print(ingredients) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment