如何避免输出看起来像日志(Python) [英] how can avoid the output to look like a log(Python)

查看:210
本文介绍了如何避免输出看起来像日志(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个老虎机游戏写一个简单的脚本codeacademy.com的教程
我想打印的东西在同一个地方,第一次打印不是以前的一切。这是可能吗?

I'm writing a simple script for a slot machine game just after a tutorial on codeacademy.com I would like to print things in the same place of the first print not everything under the previous. Is this possible?

在我的脚本中,我打印一个这样的板子:

In my script i print out a board like this:

slot = []

for row in range(9):
    slot.append(['[]'] * 9)

def print_slot(slot):
    for element in slot:
        print(" ".join(element))
print_slot(slot)

在这个董事会后,我不想为每个行动
另一个董事会,但我想采取行动发生在董事会或更好当你重新运行代码时得到的输出。

after this board i don't want another board for every action but i would like actions to happen in the board or, even better something like the output you get when you rerun the code.

如何实现呢?东西不同于印刷?
或者也许是python控制台这样工作,一个完整的程序可以不同。 (这里是真正的初学者)

how can achieve this? something different from print? or maybe is the python console thet work like this, a complete program can be different. (real beginner here)

这是我在用户输入之前的输出:

this is my output before the user input:

##- simple slotmachine -##
##- five identical win -##
##- u start with 100cr -##
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
##- simple slotmachine -##
##- five identical win -##
##- space enter 2 play -##


$ b b

用户输入之后:

here after the user input:

##- simple slotmachine -##
##- five identical win -##
##- u start with 100cr -##
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
##- simple slotmachine -##
##- five identical win -##
##- space enter 2 play -## 
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] 0 4 5 4 5 [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []
[] [] [] [] [] [] [] [] []

您可以看到上一个板仍然可见,我想在每次更改时替换一个新板。

as you can see the previous board is still visible, i would like to substitute a new board at every change.

推荐答案

这是一个例子,你可以模拟 stdout (实际上,你将使用 \r 写入旧输入回到行的开头):

This is an example how you can emulate a rewrite of an old line in stdout (actually you're going back to the start of the line using \r and writing over the old input):

#!/usr/bin/env python

import sys
import time

sys.stdout.write("This is in a line")
# force flush as stdout is flushed by default on '\n'
sys.stdout.flush()
time.sleep(2)
# rewrite line and move to next
sys.stdout.write("\rThis is still in the same line\n")

您可以使用这些VT100代码以覆盖以前的行,如此回答

You can use these VT100 codes to overwrite previous lines as suggest in this answer:

CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'

print "these"
print "are"
print "four"
print "lines"
time.sleep(2)
# go back two lines and overwrite with two new lines
sys.stdout.write(CURSOR_UP_ONE)
sys.stdout.write(CURSOR_UP_ONE)
print "five"
print "lines"
print "now"

这篇关于如何避免输出看起来像日志(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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