简体   繁体   English

python同时打印到多行

[英]printing to multiple lines at the same time python

I am making a text based game and I am trying to print this code one character at a time on each column. 我正在做一个基于文本的游戏,我试图每次在每一列上一次打印此代码一个字符。

'''############### '''###############

Let us begin... 让我们开始...

###############''' ###############'''

I can't figure out how to make it come out one column at a time. 我不知道如何使它一次出现在一栏。

Well, I still felt like answering this despite the vagueness of your question. 好吧,尽管您的问题含糊不清,但我仍然想回答这个问题。 Maybe this is what you are looking for, this prints one column at a time (one character per row): 也许这就是您要查找的内容,它一次打印一列(每行一个字符):

import subprocess
import platform
from time import sleep


def clear_screen():
    # thanks to: https://stackoverflow.com/a/23075152/2923937
    if platform.system() == "Windows":
        subprocess.Popen("cls", shell=True).communicate()
    else:
        print("\033c", end="")


# obviously you can create a function to convert your string into this
# list rather than doing it manually like I did, but that is another question :p.
views = ['#\nh\n#', '##\nhe\n##', '###\nhel\n###', '####\nhell\n####', '#####\nhello\n#####']

for view in views:
    clear_screen()
    print(view)
    sleep(0.5)

If you are already doing print(c, end='') for each character in you string, just add flush=True to the call to print() . 如果您已经为字符串中的每个字符执行了print(c, end='') ,只需将flush=True添加到对print()的调用中即可。 The sleep call will introduce enough delay so that you can see the characters print one at a time: 睡眠呼叫将引入足够的延迟,以便您可以一次看到一个字符:

>>> import time
>>> s = '''###############
... Let us begin...
... ###############'''
>>> for c in s:
...    print(c, end='', flush=True)
...    time.sleep(0.1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM