问题
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()
回答1:
Unless you want to use curses (which is another big step), you cannot go back by several lines.
BUT what you can do is clear the screen and redisplay everything.
print(chr(27) + "[2J")
clears the screen
(as stated in clear terminal in python)
回答2:
You can clear the screen before printing the board.
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
And inside the board() you can call the clearscreen() to clear the screen before printing the board.
回答3:
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()
来源:https://stackoverflow.com/questions/39016202/replace-multiline-existing-output-in-python