如何避免输出看起来像日志(Python)

2024-09-29 19:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为吃角子老虎机游戏编写一个简单的脚本代码学院.com 我想把打印的东西放在同一个地方,第一次打印下的东西不全是前一次。这可能吗?你知道吗

在我的脚本中,我打印出这样一块板:

slot = []

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

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

在这个董事会之后,我不想每个动作都有另一个董事会 但是我希望操作发生在board中,或者更好的是,类似于重新运行代码时得到的输出。你知道吗

如何才能做到这一点?和印刷品不一样吗? 或者python控制台是这样工作的,一个完整的程序可以是不同的。(这里是真正的初学者)

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

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

在用户输入之后:

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

正如你所看到的以前的董事会仍然是可见的,我想更换一个新的董事会在每一个变化。你知道吗


Tags: 代码用户in脚本forelementsimplestart
1条回答
网友
1楼 · 发布于 2024-09-29 19:20:19

这是一个如何模拟stdout中旧行的重写的示例(实际上,您将使用\r返回到行的开头,并重写旧输入):

#!/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")

您可以使用these VT100 codes覆盖前面的行,如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"

相关问题 更多 >

    热门问题