如何在循环中的同一行上打印?

时间:2012-11-11 05:38:35

标签: printing python-3.x console-output

我正在尝试在纸牌游戏的同一行打印结果,这是我想要的所需输出:

desired output

这是我得到的:

actual ouput

这是我的代码:

  for List in tableau:
        print
        print ("Row", Row, ":", end="")
        print
        Row += 1
        for x in List:
            print (x, end="")

我正在使用Python 3,谢谢。

3 个答案:

答案 0 :(得分:3)

你需要在Python 3中调用print函数:

for List in tableau:
      print()  # Right here
      print ("Row", Row, ":", end="")

      Row += 1
      for x in List:
          print (x, end="")

看看Python 2和Python 3之间输出的差异:

Python 2

>>> print

>>>

Python 3

>>> print
<built-in function print>
>>> print()

>>>

稍微更紧凑的方式就是这样:

for index, row in enumerate(tableau, start=1):
    print('Row {index} : {row}'.format(index=index, row=' '.join(row)))

答案 1 :(得分:1)

您需要将print更改为功能。

for List in tableau:
    print()
    print ("Row", Row, ":", end="")
    print()
    Row += 1
    for x in List:
        print (x, end="")

答案 2 :(得分:0)

  for List in tableau:
    print("\n")
    print ("Row", Row, ":", "")

    print("\n")
    Row += 1
    for x in List:
        print (x, end="")

这应该可以解决问题。它对我有用。

相关问题