在不同时间使用Python打印行

时间:2012-09-26 01:12:10

标签: python time

我正在做一个简单的家庭作业游戏。我想打印线条,但让它们彼此分开打印1秒钟。我该怎么做呢?

我认为会延迟打印的东西。所以喜欢

"Hello"
"My name is blahblah"
"This is blah blah"
"blah blah"
"What's your name?"

4 个答案:

答案 0 :(得分:5)

在你的情况下,

time.sleep(seconds)暂停一秒钟:

import time
strings = ["Hello","My name is blahblah","This is blah blah","blah blah","What's your name?"]

for txt in strings:
    print txt
    time.sleep(1)

答案 1 :(得分:0)

您可以使用time.sleep(1)延迟1秒

答案 2 :(得分:0)

import time

while(1):
    print("-")
    time.sleep(1)

答案 3 :(得分:0)

# coding:utf-8 -*-

import  time

def print_line(lines):
    """
        print every line in lines per 1s 
    """
    assert type(lines) in (list, tuple, str)
    if type(lines) == str:
        lines = (str,)

    for line in lines:
        print line
        time.sleep(1)


if __name__ == "__main__":
    """test print line with a simple tuple"""

    test_data = "Hello", "My name is blahblah", "This is blah blah","blah blah","What's your name?"
    print "print one sentence per second, begin ..."
    print_line(test_data)
    print "finished!"