Python how to convert total teaspoons to tablespoons, ounces, cups, and pints using modulus and floor division

86 Views Asked by At

I need to take a given amount of teaspoons and convert it to statements in which the bigger units fill first and the smaller units fill second. For example,

this is what it's supposed to look like:

total_teaspoons = 57

19 tablespoons, 0 teaspoons
9 ounces, 1 tablespoon, 0 teaspoons
1 cup, 1 ounce, 1 tablespoon, 0 teaspoons
0 pints, 1 cup, 1 ounce, 1 tablespoon, 0 teaspoons

I've been stuck on this for a while and I don't know how to work it out.

total_teaspoons = 57

pints = (total_teaspoons // 96)
cups = (total_teaspoons // 48)
ounces = (total_teaspoons) // 6
tbsps = total_teaspoons // 3
tsps = total_teaspoons % total_teaspoons

print(tbsps, tsps)

print(ounces, tbsps, tsps)
print(cups, ounces, tbsps, tsps)
print(pints, cups, ounces, tbsps, tsps)

The thing I don't get is how to change the value of tablespoon or ounces depending on whether you are including the other bigger measurements like cups or pints?

2

There are 2 best solutions below

0
bona_rivers On

This solution will work, it is not the most elegant solution, but you can get the idea on how it should work and you can play with it to improve it.

total_teaspoons = 57

pints_conversion = 96
cups_conversion = 48
ounces_conversion = 6
tbsps_conversion = 3

temp_teaspoons = total_teaspoons
pints = (temp_teaspoons // pints_conversion)
temp_teaspoons = temp_teaspoons % pints_conversion
cups = (temp_teaspoons // cups_conversion)
temp_teaspoons = temp_teaspoons % cups_conversion
ounces = (temp_teaspoons) // ounces_conversion
temp_teaspoons = temp_teaspoons % ounces_conversion
tbsps = (temp_teaspoons) // tbsps_conversion
temp_teaspoons = temp_teaspoons % tbsps_conversion
tsps = temp_teaspoons

total_tbsps = int(tbsps+ounces*ounces_conversion/tbsps_conversion+cups*cups_conversion/tbsps_conversion+pints*pints_conversion/tbsps_conversion)
print(total_tbsps, tsps)

total_ounces = int(ounces+cups*cups_conversion/ounces_conversion+pints*pints_conversion/ounces_conversion)
print(total_ounces, tbsps, tsps)

total_cups = int(cups+pints*pints_conversion/cups_conversion)
print(total_cups, ounces, tbsps, tsps)

print(pints, cups, ounces, tbsps, tsps)

The idea is that using % you get the rest of the division and so you get the remaing teaspoons to convert in the next unit.

For printing in a smaller number of units you just need to convert the unit that you won't print in the largest unit you want to print and you can do that by dividing the conversion factor of each unit you will not use by the conversion factor of the biggest unit you will use.

0
Suramuthu R On

Please Go thru the code. It is easily understandable. I've given comments only for the inportant things.

teaspoon = 222
i = 0
s = ''

while i <= 5:
    
    if teaspoon >= 96:
        pints = str(teaspoon // 96) + ' pints,'
        teaspoon = teaspoon % 96
        s = s + pints
      
    elif teaspoon in range(48, 96):
        cups =  str(teaspoon // 48) +' cups,'
        teaspoon = teaspoon % 48
        s = s + cups
      
    elif teaspoon in range(6, 48):
        ounces = str(teaspoon // 6) + ' ounces,'
        teaspoon = teaspoon % 6
        s = s + ounces
        
    elif teaspoon in range(3, 6):
        tablespoons = str(teaspoon // 3) + ' tablespoons,'
        teaspoon = teaspoon % 3
        s = s + tablespoons
        
    # if teaspoon in the range 1 and 3 give teaspoon value and break
    elif teaspoon in range(1, 3):
        teaspoon = str(teaspoon) + ' teaspoons.'
        s = s + teaspoon
        break

    #if teaspoon is zero add empty string to teaspoon and break 
    else:
        s = s + ''
        break
    
    i += 1

#The last character is comma as per our code. Replace it with fullstop
s = s[:-1] + '.'
print(s)

#Results:

#teaspoon = 104 ; Output : 1 pints,1 ounces,2 teaspoons.

#teaspoon = 96 ; Output :1 pints.

#teaspoon = 38 ; Output : 6 ounces,2 teaspoons.

#teaspoon = 185 ; Output : 1 pints,1 cups,6 ounces,1 tablespoons,2 teaspoons.

#teaspoon = 72 ; Output : 1 cups,4 ounces.

#teaspoon = 18 ; Output : 3 ounces.

#teaspoon =222 ; Output : 2 pints,5 ounces.