且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

替换python中的多行现有输出

更新时间:2023-12-04 11:43:40

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