In: Computer Science
Write a function lbs2lboz(p) that takes a non-negative number, p , that represents a weight in pounds and outputs a pair (l,o) such that l is an integer and p=l+o/16.
Write a function oz2lboz(oz) that takes a non-negative number, oz , that represents a weight in ounces and outputs a pair (l,o) such that l is an integer and oz=l*16+o.
on python
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
# method to convert pounds to pounds and ounces
def lbs2lboz(p):
# extracting integer part from p
l = int(p)
# multiplying remaining value by 16 to get value in ounces
o = (p - l) * 16
# returning a pair containing value in pounds and ounces
return (l, o)
# method to convert ounces to pounds and ounces
def oz2lboz(oz):
# dividing by 16 and fetching integer part to get pounds
l = int(oz / 16)
# subtracting above value from oz to get remaining ounces
o = oz - (l * 16)
# returning a pair containing value in pounds and ounces
return (l, o)
# code for testing
print(lbs2lboz(34.50)) #should be 34 pounds and 8 ounces
print(oz2lboz(99.5)) #should be 6 pounds and 3.5 ounces
#output
(34, 8.0)
(6, 3.5)