Python coding
This is an exercise in coding with some repetition, with some output formatting and if-statements thrown in.
Problem Description
Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups.
This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself.
Here is an example recipe that comes from the story "Chitty Chitty Bang Bang", written by Ian Fleming, who is much better known for introducing the world to James Bond:
This is a recipe scaler for serving large crowds! Enter one ingredient per line, with a numeric value first. Indicate the end of input with an empty line. 4 tbsp cocoa 1/4 pound butter 2 tsp corn syrup 1 can evaporated milk 1 tsp water Here is the recipe that has been recorded 4 tbsp cocoa 1/4 pound butter 2 tsp corn syrup 1 can evaporated milk 1 tsp water How many does this recipe serve? 16 How many people must be served? 25 Multiplying the recipe by 2 8 tbsp cocoa 2/4 pound butter 4 tsp corn syrup 2 can evaporated milk 2 tsp water Serves 32
NOTE: The recipe rounds upwards, since it is usually not practical to obtain fractional cans or fractional eggs, etc.
A Couple Free Program Tricks
Attractive user-friendly output is rather straightforward, with the help of Python's string formatting features. User-friendly input is a little trickier, so here is a peek at a little of the instructor's code:
First trick:
The name of an ingredient might be more than one word. This will place all of the extra words into a single string variable 'item':
quant, unit, item = line.split(' ',2) # pull off at most 2 words from the front
Second trick:
Sometimes the measure will be fractional. We can tell that if there is a slash in there. Not having a slash can be treated the same way as a fraction with a denominator of 1.
if '/' in quant: numer, slash, denom = quant.partition('/') # get the parts of the fraction
The rest is left up to the student -- since this is a string operation and this fraction represents a number.
Extra Credit Option
No doubt the output seems to be a little strange to ask for 2/4 pounds of butter. One might think it would be better to ask for 1/2.
Modify the program so that all fractions are reduced to their lowest terms.
Also, express all improper fractions (where the numerator exceeds the denominator) as mixed fractions. Scaling this recipe by a factor of 10 would ask for 2 1/2 pounds of butter.
And of course, the resulting output should still be easy to read.
Other Guidelines
Clarity of code is still important here -- and clear code is less likely to have bugs.
In particular, there should be very good and clear decisions in the code.
And there will be a penalty for usage of break or continue statements.
Planning out the design of the solution before diving into code will help!
The code is tested and run on python 2.7
I have coded the extra credit portion even. The code is clear and lucid. Kudos.
Here we go
#!/usr/bin/python
import math
print "This is a recipe scaler for serving large crowds!\n\nEnter
one ingredient per line, with a numeric value first.\nIndicate the
end of input with an empty line."
def gcd(a, b):
if a == 0:
return b
elif b == 0:
return a
return gcd(b % a, a)
emptyline = False
data = [] #this stores the data
while emptyline == False:
line = raw_input()
if line == "":
emptyline = True
else:
quan, unit, item = line.split(' ',
2)
num = quan
den = "1"
if '/' in quan:
num, den =
quan.split('/', 1)
data.append( [num, den, unit, item])
print "Here is the recipe that has been recorded"
for line in data:
if line[1] == "1":
print line[0], " ", line[2], " ",
line[3]
else :
print line[0] + "/" + line[1], " ",
line[2], " ", line[3]
print "How many does this recipe serve?",
a = float(raw_input())
print "How many people must be served?",
n = float(raw_input())
m = int(math.ceil(n/a))
print "Multiplying the recipe by ", m
print ""
for line in data:
if line[1] == "1": #denominator is 1
print int(line[0])*m, " ", line[2],
" ", line[3]
else : #its a fraction
#check if its improper after
multiplication
num, den = int(line[0]) * m,
int(line[1])
#simplify first
hcf = gcd(num, den)
num, den = num//hcf, den//hcf
#improper fraction
conversion
if den == 1:
print num, " ",
line[2], " ", line[3]
elif num > den:
num, rem = (num
// den), num - (num // den) * den
print num,
str(rem)+ "/" + str(den), " ", line[2], " ", line[3]
else :
print str(num) +
"/" + str(den), " ", line[2], " ", line[3]
Python coding This is an exercise in coding with some repetition, with some output formatting and...
Answer the following questions using the next chart Recipe Detail and Cost Card Selling Price: Item Portion Cost: Maritime Clam Chowder Menu Dinner Portion 8 oz Quantity Unit Food Cost 96: 25% Yield Date 2017-01-16 6 portions Cost Calculation $4.21 Ext Ingredients Butter $4.99 $1.70 $2.79 $5.13 $2.99 30g 350g kg Salt Pork, Diced Onion, diced 120 Celery, diced 2.5kg 51g 200ml 500ml Flour Clam Stock $1.68 Milk 2% 300mlL sprig 25 springs $2.50 1 sprig 25 springs$2.50 Parsley Chopped...
Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditional and iterative control structures that repeat actions as needed. The unit measurement is missing from the final output and I need it to offer options to lbs, oz, grams, tbsp, tsp, qt, pt, and gal. & fl. oz. Can this be added? The final output...