-3

I like exploring with python and I recently watched the matrix so I wanted to try something out.

Say I was going to do this:

While True:
    print ("010101010101010101")
    print ("101010101010101010")

If you run that it will obviously keep printing that until you abort it. You will also see that the numbers blend together because they are going to fast.

I'm not asking for it to run the while true every second or so, I just kinda want it to move a little bit slower. Does anyone have a function or can tell me what to run?

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
KwaziMoto
  • 19
  • 2
  • 6
    Use `time.sleep()` for some seconds / milliseconds. – Cory Kramer Aug 17 '14 at 17:12
  • @cyber not a duplicate. I specifically said im not looking for it to run every couple of seconds because I already looked at that question. – KwaziMoto Aug 17 '14 at 17:13
  • 1
    @KwaziMoto - could you clarify what you mean? To me, it looks like the link provides a solution that would exactly solve your problem -- it adds a delay in between each print statement, effectively slowing it down. – Michael0x2a Aug 17 '14 at 17:15
  • @KwaziMoto - what do you mean by 'smoother'? On the command line, you can only add text character by character or line by line. Are you trying to make the text scroll down smoothly, rather then just appearing? Or something else? – Michael0x2a Aug 17 '14 at 17:20

2 Answers2

1

This will print a string character by character, it will also simulate typewriting waiting a random time between each character from 0.1 to 0.2 second:

import random
import time
import sys

def slowprint(str):
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(random.random() * 0.1 + 0.1)
    print ''

Then use it like:

while True:
    slowprint("010101010101010101")
    slowprint("101010101010101010")

Pretty fun actually. The possibilities are endless:

message = "all work and no play makes jack a dull boy"
while True:
    print ' ' * random.randrange(15),   # indent chaotically 
    slowprint("".join(random.choice([c.upper(), c]) for c in message))

Output

        aLl wORk And nO PLAy maKEs JacK A DULL BoY
          ALl wOrK and No pLay MAkEs Jack A dULL BOY
  ALl WOrk and No PLaY maKes JAck a DuLL bOY
         all wOrk ANd no pLAY MAKes JacK a DULL bOy
all Work and No plAY mAkES JACk a dulL bOY
      all WOrk ANd nO Play MAkEs JacK a duLL BOY
alL WorK aND no plAY makeS jAcK A DULL boy
enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
  • Suggestion: Just using `time.sleep(random.random())` seems like it might be a bit too slow, maybe `time.sleep(random.random() * 0.1)`? – Nick Meyer Aug 17 '14 at 17:29
0
import string
import time
tab = string.maketrans("01","10")
binx = "010101010101010101"
while True:
    binx = binx.translate(tab)
    print binx
    time.sleep(0.2)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179