Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python IDLE 3.6.2 shell coding This is an exercise in coding with some repetitio

ID: 3591590 • Letter: P

Question

Python IDLE 3.6.2 shell 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:

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

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.

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 that i have already,,,

import math
import fractions
items=list() #list of all the ingredients along with quantity
item=list() #individual element in the list items
line=input("Enter one ingredient per line, with a numeric value first. Indicate the end of input with an empty line ")
while line!="":
item = line.split(" ",2)
items.append(item)
line = input()

print("Here is the reciepe recorded")
for i in items:
for j in i:
print(j,end=" ")
print()

print()
num = int(input("How many people this reciepe serve? "))
people = int(input("How many people you must serve? "))

#finding the number to which the quantity is to be multiplied
multiply = math.ceil(people/num)
print("Multiplying the reciepe by " + str(multiply))
print()

#updating quantity and printing
for i in items:
count = 0
#i[0] is the quantity where i is the element in items
for u in i[0]:
if u=="/":
count=1

#checking for interger or fraction
if count!=1:
#updating quantity
i[0]=int(multiply*int(i[0]))
else:
numer, slash, denom = i[0].partition('/')
numer=int(numer)
denom=int(denom)
numer=numer*multiply
#for interger quantity
if numer % denom == 0:
i[0]=str(int(numer/denom))

#for mixed fraction
elif numer>denom:
mix = str(int(numer//denom))
rem = numer%deno
#finding greatest common devisor for remainder and denominator
div=fractions.gcd(rem,denom)
#updating quantity
i[0]= mix + " " + str(int(rem/div))+"/"+str(int(denom/div))
#for normal fraction
else:
#finding greatest common devisor for numeratr and denominator
div=fractions.gcd(numer,denom)
#updating quantity
i[0]=str(int(numer/div))+"/"+str(int(denom/div))

#printing quantity
for p in i:
print(p,end=" ")
print()
  
print()
print("Serves " + str(num*multiply))

Explanation / Answer

import math
import fractions
items=list() #list of all the ingredients along with quantity
item=list() #individual element in the list items
line=input("Enter one ingredient per line, with a numeric value first. Indicate the end of input with an empty line ")
while line!="":
item = line.split(" ",2)
items.append(item)
line = input()
print("Here is the reciepe recorded")
for i in items:
print(' '.join(i))
print()

num = int(input("How many people this reciepe serve? "))
people = int(input("How many people you must serve? "))
#finding the number to which the quantity is to be multiplied
multiply = math.ceil(people/num)
print("Multiplying the reciepe by " + str(multiply))
print()
#updating quantity and printing
for i in range(0, len(items)):
quant = items[i][0] # = int(items[i][0])*multiply
if '/' in quant:
number, slash, denom = quant.partition('/') # get the parts of the fraction
number = int(number)*multiply
denom = int(denom)
f = fractions.Fraction(number, denom)
number = f.numerator
denom = f.denominator
if number > denom:
a = number // denom
number = number % denom
items[i][0] = str(a) + " " + str(number) + "/" + str(denom)
else:
items[i][0] = str(number) + "/" + str(denom)
else:
items[i][0] = str(int(items[i][0])*multiply)

for i in items:
print(' '.join(i))
print()

print("Serves " + str(num*multiply))

# code link: https://paste.ee/p/JrI1p

# please do give a thumbs up.

Sample run