Question

Problem Description Many recipes tend to be rather small, producing the fewest number of servings that...

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.

Your program must obtain a complete recipe (not necessarily this one), echo it with good formatting, and then scale it up as shown above.

A Couple Free Program Hints

Attractive user-friendly output is rather straightforward, with the help of Python's string formatting features. User-friendly input is a little trickier, but the split function from Unit 2 can be very helpful:

First hint:

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 hint:

Sometimes the measure will be fractional. We can recognize that if the number contains a slash.

if '/' in quant: 
    numer, denom = quant.split('/')        # 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. There is a function in the Python math module named gcd that can help with this. (You can email course personnel if you need help accessing the math features.)

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 simplest solutions would use a list, but without any indexing on that list
(or use of range() to get those indexes).  Let Python help you fill and traverse the recipe.

Storing the entire recipe in a single list before splitting things up often produces much simpler programs than trying to store everything into multiple separate lists!

IMPORTANT NOTE: As above, the recipe is provided as input to the program -- it is not part of the program itself. The program may not assume it knows what the ingredients are, or how many there are, or which ingredients have fractions and which ones do not. It must work for any number of valid input lines.

Homework Answers

Answer #1

contents = []
flag = True
print ('Enter one ingredient per line, with a numeric value first.\n')
while flag:
try:
line = raw_input("")
contents.append(line)
except EOFError:
flag = False

quant = []
unit = []
item = []

for line in contents:
q, u, i = line.split(' ',2)
quant.append(q)
unit.append(u)
if '\r' in i:
item.append(i.split('\r')[0])
else:
item.append(i)

print ('\nHere is the recipe that has been recorded')
for i in range(len(unit)):
print (quant[i] + ' ' + unit[i] + ' ' + item[i])

av = int(input('How many does this recipe serve? '))
rq = int(input('How many people must be served? '))

mul = 0
if rq % av > 0:
mul = (rq/av) + 1
else:
mul = (rq/av)

print ('\nMultiplying the recipe by ''{}{}'.format('', mul))

for i in range(len(unit)):
if '/' in quant[i]:
n = 0
d = 0
n1 = 0
d1 = 0
numer, slash, denom = quant[i].partition('/')
n = int(numer) * mul
d = int(denom)
if d % n == 0 :
n1 = n/(d/n)
d1 = d/(d/n)
print ('{}{}'.format('', n1) + slash + '{}{}'.format('', d1) + ' ' + unit[i] + ' ' + item[i])Â Â
else:
print ('{}{}'.format('',(int(quant[i]) * mul)) + ' ' + unit[i] + ' ' + item[i])

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT