2

基本的に、私は大学から宿題を出されており、ユーザーは x 量のオンスを入力する必要があり、すべて変換され、ストーン、ポンド、残りのオンスで画面に出力されます。私はこれに1週間近く立ち往生しています。これまでに私が管理したコードは次のとおりです。

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")

石のビットは完璧に機能しますが、残りのオンスをどのように取得してポンドに変換し、残りをオンスに変換するのだろうか?

4

2 に答える 2

0

あなたはあなたと親密です。これは機能します:

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))

ただし、石は伝統的に小数ではなく有理数で表現されるため、標準 Python ライブラリのFractionsモジュールを使用できます。

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))

入力、出力の例:

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

または、英国の伝統的な方法で石の重量を印刷することもできますN Stone XX (pounds)

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

どちらが印刷されますか:

Enter your weight in ounces: 2528
11 Stone 4
于 2013-09-11T21:18:49.157 に答える