【问题标题】:Replace multiline existing output in python替换python中的多行现有输出
【发布时间】:2016-08-18 10:45:58
【问题描述】:

在 python 3 中,已经有关于如何用另一个替换现有输出的答案。 这些答案建议在 print('hello', end='\r') 中使用 end='\r' ad 这当然有效,但它只适用于一行。

在我下面发布的程序中,我首先输出 5 行表示一个表格矩阵。要求用户键入一个数字 (1-3),然后在用户指定的位置再次打印矩阵并带有“X”。

但正如您所见,矩阵打印在初始矩阵的下方。如何替换现有的输出?

如果我使用 end = '\r' 它只会将光标移动到行首。但这对我不起作用,因为我想打印 5 行,然后将光标移动到第一行的开头,而不是第五行的开头(就像 end='\r' 一样)。

如何在 python 中实现这一点?

from __future__ import print_function
list1=[' '*11,'|',' '*7,'|',' '*10] 

def board():
    print ('\t \t | \t \t |')
    print (list1)
    print ('\t \t | \t \t |')
    #print '\n'
    print

def playerone():    
    player1 = raw_input("PLAYER1: enter your position 1-3: ")
    if (player1 == '1'):
        list1[0]=' '*5 + 'X'+' '*5
    elif (player1=='2'):
        list1[2]=' '*3 + 'X'+' '*3
    elif (player1=='3'):
        list1[4]=' '*3 + 'X'+' '*6
    else:
        playerone()

print ('our board is : ')
board()

playerone()

print ('our board is :')
board()

【问题讨论】:

    标签: python python-2.7 python-3.x ipython jupyter-notebook


    【解决方案1】:

    除非你想使用curses(这是又一大步),否则你不能返回几行。

    但是您可以做的是清除屏幕并重新显示所有内容。

    print(chr(27) + "[2J")
    

    清屏

    (如clear terminal in python中所述)

    【讨论】:

    • 我正在使用 jupyter。在 python 3 中,这不起作用。至少在我的情况下。
    【解决方案2】:

    您可以在打印板之前清除屏幕。

    def clearscreen(numlines=100):
    """Clear the console.
    numlines is an optional argument used only as a fall-back.
    """
    import os
    if os.name == "posix":
        # Unix/Linux/MacOS/BSD/etc
        os.system('clear')
    elif os.name in ("nt", "dos", "ce"):
        # DOS/Windows
        os.system('CLS')
    else:
        # Fallback for other operating systems.
        print '\n' * numlines
    

    在 board() 内部,您可以调用 clearscreen() 在打印板之前清除屏幕。

    【讨论】:

      【解决方案3】:

      如果尝试使用终端的 clear 命令使用便携式解决方案,例如:

      from __future__ import print_function
      import os
      
      class Game:
      
          def __init__(self):
              self.running = True
              self.list1 = [' ' * 11, '|', ' ' * 7, '|', ' ' * 10]
      
          def clear_sceen(self):
              os.system('cls' if os.name == 'nt' else 'clear')
      
          def draw_board(self):
              print('our board is : ')
              print('\t \t | \t \t |')
              print(self.list1)
              print('\t \t | \t \t |')
      
          def check_inputs(self):
              player1 = raw_input("PLAYER1: enter your position 1-3: ")
      
              if (player1 not in ['1', '2', '3']):
                  self.running = False
              else:
                  print(chr(27) + "[2J")
      
                  if (player1 == '1'):
                      self.list1[0] = ' ' * 5 + 'X' + ' ' * 5
                  elif (player1 == '2'):
                      self.list1[2] = ' ' * 3 + 'X' + ' ' * 3
                  elif (player1 == '3'):
                      self.list1[4] = ' ' * 3 + 'X' + ' ' * 6
      
          def run(self):
              self.clear_sceen()
      
              while self.running:
                  self.draw_board()
                  self.check_inputs()
      
              print(
                  '\nGame ended! you should have pressed numbers between 1-3 :P')
      
      if __name__ == "__main__":
          g = Game()
          g.run()
      

      【讨论】:

        猜你喜欢
        • 2020-08-18
        • 2016-01-13
        • 2019-09-30
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 1970-01-01
        • 2021-10-23
        • 2021-08-31
        相关资源
        最近更新 更多