你和你的很亲近。这有效:
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