替换python中的多行现有输出 [英] Replace multiline existing output in python

查看:455
本文介绍了替换python中的多行现有输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python 3中,已经有关于如何将现有输出替换为其他输出的答案. 这些答案建议在印刷品中使用end ='\ r'广告('hello',end ='\ r') 当然这是可行的,但仅适用于一行.

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

但是,正如您所看到的,矩阵被打印在初始矩阵的下方.如何替换现有输出?

如果我使用end ='\ r',它将仅将光标移动到该行的开头.但这对我不起作用,因为我要打印5行,然后将光标移动到第一行的开头,而不是第五行的开头(如end ='\ r'那样). /p>

如何在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()

解决方案

如何尝试使用终端的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()

In python 3 , there have been answers about how to replace an existing output with another. These answers suggest the use of end='\r' ad in print('hello', end='\r') This is working ofcourse, but it works only for one line .

In the program that I post below, I first output 5 lines which is the representation of a table matrix. The user is asked to type one number (1-3) and then the matrix is printed again with an 'X' in the position that the user indicated.

But as you can see, the matrix is printed below the initial one. How can I replace the existing output ?

If I use the end = '\r' it will just move the cursor to the beginning of the line. But this will not work for me because I want to print 5 lines , and then move the cursor to the beginning of the first line, and not to the beginning of the fifth line (as the end='\r' does).

How could I achieve this in 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()

解决方案

What about trying to use a portable solution using terminal's clear command, for example:

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

这篇关于替换python中的多行现有输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆