5 Replies - 5195 Views - Last Post: 29 August 2011 - 08:04 PM Rate Topic: -----

#1 sociallyawkwardchocobo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 22-June 11

Single-Line Print Delay

Posted 06 August 2011 - 04:12 PM

Hello all. I've got a tricky problem I need a little help with.

I'm working on a little IRC bot called ChocoBot, and my aim in its creation is to make it fun to use. So I'm trying to make it user-friendly by trying to emulate more human-like "speech"--for example, adding in pause for dramatic suspense. That being said, I'll elaborate on what it is specifically I'm trying to do:

In this case, I'm trying to add in "awkward pause" (you know, like when you say "Uh..." after finding out that your girlfriend is actually your cousin, and then briefly exclaiming "I need to take a shower."). The way I want it to appear is by first printing the initial statement (in this case, "Uh"), then following that directly with a "." after a slight delay, and finally ending the sentence in an abrupt and awkward way ("I need to take a shower."). I've tried a couple of different things; this is what I have so far:

from time import sleep

t1 = "Uh"
t2 = ". . ."
t2s = t2.split()
print(t1)
for d in t2s:
    sleep(1)
    print(d)
print(" I need to take a shower.")



What this gives me is something like this:

Uh (pause)
. (pause)
. (pause)
.
I need to take a shower.

I've tried replacing print() with sys.stdout.write(), but while the output stays on one line, the delay is saved until the for loop finishes. Which is kind of annoying.

If there's a better way to do this, please let me know. Thanks!

Is This A Good Question/Topic? 1
  • +

Replies To: Single-Line Print Delay

#2 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Single-Line Print Delay

Posted 07 August 2011 - 01:33 AM

In your interpreter, use help(print). You'll notice there's mention of an end parameter, with a default of '\n'. You can set that to a blank string for the lines you want, so it won't print to a new line each time.

As for the output not being displayed until the "shower" line is printed, that's because the output buffer isn't being flushed until it gets to the new line character. If you add sys.stdout.flush() to the end of the for loop, that should work for you.

This post has been edited by Hiram: 07 August 2011 - 01:34 AM

Was This Post Helpful? 2
  • +
  • -

#3 sociallyawkwardchocobo  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 22-June 11

Re: Single-Line Print Delay

Posted 07 August 2011 - 03:06 PM

Thanks, I think that fixed it!
Was This Post Helpful? 0
  • +
  • -

#4 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Single-Line Print Delay

Posted 08 August 2011 - 06:00 AM

Glad I could help! :3
Was This Post Helpful? 0
  • +
  • -

#5 CryptochildXR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-January 10

Re: Single-Line Print Delay

Posted 24 August 2011 - 05:22 AM

I've noticed something in your code, there's a place where you the '.split()'

from time import sleep  

    

t1 = "Uh" 

t2 = ". . ." 

t2s = t2.split() #this is redundant 

print(t1)  

for d in t2s:  

    sleep(1)  

    print(d)  

print(" I need to take a shower.") 




a string is afterall an array of characters so the following code will have sufficed

from time import sleep  

    

t1 = "Uh" 

t2 = ". . ." 

 

print(t1)  

for d in range(len(t2)):  

    sleep(1)  

    print(t2[d-1], end=" ")  

print(" I need to take a shower.") 



where d staores the length of a string that contains ". . ." which is 5 and since a computer counts from 0 then the fifth element will be indexed 4 therefore [d-1]
Was This Post Helpful? 0
  • +
  • -

#6 Python_4_President  Icon User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 321
  • Joined: 13-August 11

Re: Single-Line Print Delay

Posted 29 August 2011 - 08:04 PM

This seems more natural to me...

import time


def pause_on_the_eyes(letter_collection):
    rest = letter_collection.split("I")[1].lstrip()
    print "I..."
    time.sleep(1.3)
    print rest
    
    
pause_on_the_eyes("I want to own all works by Michael Faraday.")


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1