Domanda

I'm trying to print a text gradually.

import time

print time.sleep(1),'Whats ', time.sleep(1),'your', time.sleep(1),'PROBLEM?'

No errors occur. but I do get.

None Whats  None your None PROBLEM?

Is this possible to achive? If so any better ways to do it or a solution? Thanks.

È stato utile?

Soluzione 2

You cannot print a function such as time.sleep(1) because it doesn't return anything, therefore, the None. Instead, try the following:

from sys import stdout
from time import sleep

text = "What's your problem?"
text = text.split()

for k in text:
        stdout.write(k+' ')
        stdout.flush()
        sleep(1)

print '\n'

Output:

What's $ your $ problem?

The $ signifies the pause of 1 second. If you use the following code:

import time

text = "What's your PROBLEM?"
text = text.split()            #text is a list now: ["What's", "your", "PROBLEM?"]

for i in text:
    print i,
    time.sleep(1)

It prints everything at once after 3 seconds.

Look here

Altri suggerimenti

You should put it in a loop:

import time

text = "What's your PROBLEM?"
text = text.split()            #text is a list now: ["What's", "your", "PROBLEM?"]

for i in text:
    print i,
    time.sleep(1)

[OUTPUT]
What's (1 sec) your (1 sec) PROBLEM?      #the (1 sec) doesn't actually print

time.sleep(1) returns the null value: None. The following code would produce the same output: print None, 'Whats ', None,'your', None,'PROBLEM?'. Though it wouldn't pause.

What you want to do here is loop over the different blocks of text text that you want to print and sleep after each printing (just as sshashank124 recommends).

import time
words = ["What's", "your", "PROBLEM?"]

for word in words:
  print word,
  time.sleep(1)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top