KrisBox
KrisBox

Reputation: 21

Ounces to Stones, lbs and ounces in python

Basically I have been given homework from college and the user has to input an x amount of ounces and it all gets converted and printed out to the screen in Stones, lbs and remaining ounces. I have been stuck on this for nearly a week. Here is the code I've managed to do so far:

inp = int(input("Enter your weight in ounces: "))

stones = int(inp / 224)
inp1 = int(inp - (stones * 14))
pounds = int(inp1 % 16)

print(stones ,"Stones", pounds, "Pounds")

The stones bit works perfectly but I am wondering how you get the remaining ounces and convert them into lbs then the remaining in ounces?

Upvotes: 2

Views: 4211

Answers (2)

dawg
dawg

Reputation: 104072

You are close with yours. This works:

inp = float(input("Enter your weight in ounces: "))

stones = inp / 224
pounds = stones * 14

print('{:.2f} Ounces is {:.2f} Stones or {:.2f} Pounds'.format(inp, stones, pounds))

However, since stone are traditionally expressed with rational numbers, not decimals, you can use the Fractions module in the standard Python library:

import fractions

inp = int(input("Enter your weight in ounces: "))

if inp>=14*16:
    stones, f=inp // 224, fractions.Fraction(inp%224, inp)
    pounds, oz = inp // 16, inp%16
    outs=str(stones)
    if abs(f)>.01:
        outs+=' and {}/{}'.format(f.numerator, f.denominator)

    outs+=' Stone'   
    outs+=' or {} Pounds'.format(pounds)
    if oz>=1:
        outs+=' {} ounces'.format(oz)    
    print(outs)

else:
    f=fractions.Fraction(inp, 224)
    pounds, oz = inp // 16, inp%16

    print('{}/{} Stone or {} Pounds {} ounces'.format(
          f.numerator, f.denominator, pounds, oz))

Example input, output:

Enter your weight in ounces: 1622
7 and 27/811 Stone or 101 Pounds 6 ounces

Enter your weight in ounces: 17
17/224 Stone or 1 Pounds 1 ounces

Enter your weight in ounces: 2240
10 Stone or 140 Pounds

Enter your weight in ounces: 3450
15 and 3/115 Stone or 215 Pounds 10 ounces

Or you can do the traditional British way to print Stone weight by N Stone XX (pounds):

inp = int(input("Enter your weight in ounces: "))
print('{} Stone {}'.format(inp//224, inp%224//16))

Which prints:

Enter your weight in ounces: 2528
11 Stone 4

Upvotes: 0

Ankur Ankan
Ankur Ankan

Reputation: 3066

The better way would be to convert the ounces into pounds first and then convert the pounds into stones.

def convert(total_ounces):
    ounces = total_ounces % 16
    total_pounds = total_ounces//16      # 1 pound = 16 ounces
    pounds = total_pounds % 14
    stones = total_pounds//14            # 1 stone = 14 pounds
    print stones, " stones ", pounds, "pounds", ounces, " ounces"


>>> convert (110)
    0  stones  6 pounds 14  ounces

>>> convert (500)
    2  stones  3 pounds 4  ounces

And the problems with your code:

inp = int(input("Enter your weight in ounces: "))

stones = int(inp / 224)                    # Here you get the maximum no of stones. You 
                                           # should better be using inp // 224 rather 
                                           # that int(inp / 224).
inp1 = int(inp - (stones * 14))            # Firstly, since both inp and stones*14 would      
                                           # be int so there is no need for using int(). 
                                           # and what I think you are trying to do here  
                                           # is finding the remaining no of ounces, so 
                                           # you should be doing something like
                                           # inp1 = inp - stones * 14 * 16
pounds = int(inp1 % 16)                    # again here there is no need of using int. 
                                           # And inp1 % 16 should return the number of 
                                           # ounces not pounds. Pounds should be inp1 // 16 .


print(stones ,"Stones", pounds, "Pounds")

Upvotes: 1

Related Questions