不太确定该问题的标题,但基本上我想知道是否有办法做到这一点:
Feet Meters | Meters Feet
---- ------ | ------ ----
1 0.305 | 1 3.281
2 0.61 | 2 6.562
3 0.914 | 3 9.843
4 1.219 | 4 13.123
5 1.524 | 5 16.404
6 1.829 | 6 19.685
7 2.134 | 7 22.966
8 2.438 | 8 26.247
9 2.743 | 9 29.528
10 3.048 | 10 32.808
执行以下操作:
Feet Meters | Meters Feet
---- ------ | ------ ----
1 0.305 | 1 3.281
2 0.61 | 2 6.562
3 0.914 | 3 9.843
4 1.219 | 4 13.123
5 1.524 | 5 16.404
6 1.829 | 6 19.685
7 2.134 | 7 22.966
8 2.438 | 8 26.247
9 2.743 | 9 29.528
10 3.048 | 10 32.808
我的代码:
print("Feet Meters | Meters Feet\n"+
"---- ------ | ------ ----")
counter = 1
for i in range(10):
print(counter, " ", round(conversions.feet_to_meters(counter), 3), " | ", counter, " ", round(conversions.meters_to_feet(counter), 3))
counter += 1
So I just want to change the formatting of the answer so everything lines up,
也许我只是放屁了,但我现在想不出办法。
答案 0 :(得分:1)
格式字符串可能是最简单的选择。另外,它可以为您做四舍五入:
for i in range(10):
print("{0:<7}{1:<9.3f}| {0:<7}{2:.3f}".format(counter, conversions.feet_to_meters(counter), conversions.meters_to_feet(counter)))
例如:
print("{0:<7}{1:<9.3f}| {0:<7}{2:.3f}".format(1, 0.3051, 3.2812))
print("{0:<7}{1:<9.3f}| {0:<7}{2:.3f}".format(2, 0.6100, 6.5621))
输出:
1 0.305 | 1 3.281
2 0.610 | 2 6.562
答案 1 :(得分:0)
我认为您可能想要使用str.format()
的答案。