user1692517
user1692517

Reputation: 1152

Have Python print lines at different times

I'm making a simple game for homework. I would like to print lines, but have them print 1 second apart from each other. How would I go about doing that?

Something that would delay the prints I guess. So like

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

Upvotes: 1

Views: 141

Answers (4)

moxwose
moxwose

Reputation: 1

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

Upvotes: 0

Dave
Dave

Reputation: 3956

import time

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

Upvotes: 0

TJD
TJD

Reputation: 11896

You can use time.sleep(1) to give a 1 second delay

Upvotes: 0

mawueth
mawueth

Reputation: 2806

time.sleep(seconds) pauses for a second, in your case:

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)

Upvotes: 5

Related Questions