Creating a dynamic recipe scaler in python using branching logic

This program was written in 2021 as part of an introductory programming course at Penn State – University Park


The purpose of this program is to create a dynamic recipe scaler that accepts an unlimited number of inputs from the user. The output is a dynamically formatted recipe utilizing mixed fractions that have been scaled according to user needs.

Learning Outcomes:

  1. Utilize lists, list functions, and list indexes
  2. Address a multi-branched logic problem and determine most concise operational routes for solutions to all branches
  3. Format output with mixed fractions, text, and multiple columns
print("This is a recipe scaler for serving large crowds!")
print("")

#setting up lists to hold keyboard inputs
quant_list = []
unit_list = []
item_list = []

#KEYBOARD INPUTS
raw_line = input("""Enter one ingredient per line, with a numeric value first.
Indicate the end of the input with an empty line:
""")
while raw_line != "":                              
#will end input after detecting empty line
    if raw_line.count(" ") == 1:                
     #handles unitless ingredients (such as eggs)
        quant, item = raw_line.split(' ')
        quant_list.append(quant)
        unit_list.append(" ")
        item_list.append(item)
    else:
        quant, unit, item = raw_line.split(' ',2) 
        #split the line input into 3 values
        #next add values to appropriate lists
        quant_list.append(quant)                    
        unit_list.append(unit)
        item_list.append(item)
    raw_line = input("") 
    #repeat input for next ingredient                               

#prints recipe with formatting
print("Here is the recipe that has been recorded:")
k=0
while k < len(quant_list):
    print(format(quant_list[k], "^7") + format(unit_list[k], "8") + format(item_list[k]))
    k = k + 1


#KEYBOARD INPUTS
servings = int(input("How many does this recipe serve? "))
people = int(input("How many people must be served? "))

#calculate how much to multiple recipe by
multiplier = people//servings
if people % servings != 0:               
    multiplier = multiplier +1
    #forces the program to always round up
print(f"""Multiplying the recipe by {multiplier}
""")

Because recipes frequently utilize fractions (like 1/2 tsp vanilla extract or 3/4 cup flour), scaling the recipe is more difficult than simply multiplying the inputted quantity numbers by the calculated multiplier.

There are a few ways the program can play out:

  1. INTEGERS: the easiest case, requires simply multiplying quantity by multiplier.
  2. FRACTIONS: requires multiplying only the numerator, then may or may not require simplifying
    1. FRACTIONS <1: these cannot be mixed and can be added directly to the output
    2. FRACTIONS =<1: these need to be simplified
      1. MIXED: will result in a combination of integers and fractions
      2. WHOLE: simplify to whole number integer
scaled_list = []
for i in quant_list:
#separate integers from fractions based on presence of "/"
    if "/" in i:                                          
        numer, denom = i.split('/')
        numer = int(numer)
        denom = int(denom)
        numer = numer * multiplier

        #separate out fractions that need to be simplified
        if numer // denom >= 1:              
            whole = numer // denom
            whole = str(whole)

            #identifies and simplifies mixed fractions
            if numer % denom != 0:           
                part_numer = numer % denom
                part_numer = str(part_numer)
                denom = str(denom)
                remainder = part_numer + "/" + denom
                mixed_fraction = whole + " " + remainder

            #takes remaining fractions and simplifies to whole numbers
            else:                                         
                mixed_fraction = whole
            scaled_list.append(mixed_fraction)

        #takes fractions <1 and assembles them for output
        else:                                             
            numer = str(numer)
            denom = str(denom)
            fraction = numer + "/" + denom
            scaled_list.append(fraction)

    #remaining numbers are integers
    #can be multiplied and passed to output
    else:                                                
        i = int(i)  
        i = i * multiplier
        scaled_list.append(i)

#OUTPUT
#print new recipe
n=0
while n < len(quant_list):
    print(format(scaled_list[n], "^7") + format(unit_list[n], "8") + format(item_list[n]))
    n = n + 1

#print number of servings for scaled recipe
print(f"""
Serves {servings * multiplier}""")

This is how the final program looks:

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *