Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,从循环到空间的方法都是一样的_Python_Python 3.x - Fatal编程技术网

Python,从循环到空间的方法都是一样的

Python,从循环到空间的方法都是一样的,python,python-3.x,Python,Python 3.x,我不确定该如何命名这个问题,但基本上我想知道是否有办法做到这一点: 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

我不确定该如何命名这个问题,但基本上我想知道是否有办法做到这一点:

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, 

也许我只是大脑放屁,但我现在想不出办法

格式化字符串可能是最简单的选择。作为额外的奖励,它可以为您进行四舍五入:

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

我想这个答案,利用str.format(),可能就是你想要的

资料来源:

您通常可以不使用空格而使用制表符,例如
print('1\t2\t3')
。这个问题已经有了类似的答案。检查Python中的str.format()和字符串格式,等等。
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