6

ok, so I am doing this tiny countdown function in vpython, the way I am doing it now is

import time
print "5"
time.sleep(1)
print "4"
time.sleep(1)
print "3"
time.sleep(1)
print "2"
time.sleep(1)
print "1"
time.sleep(1)
print "0"
time.sleep(1)
print "blastoff"

Of course, this is not actually my code, but it demonstrates it fairly well. so what I want to do is instead of printing it 5 4 3 2 1 Blastoff I want 54321 Blastoff on the same line. How would I wait for a second and print the charecter on the same line. please let me know, It would be a great help

3 Answers3

3

Try this:

import time

for i in range(5, 0, -1):
    print i, # print in the same line by adding a "," at the end
    time.sleep(1)
    if i == 1:
        print 'Blastoff!'

It'll work as expected:

5 4 3 2 1 Blastoff!

EDIT

... Or if you want to print all without spaces (which is not clearly stated in the question):

import time
from __future__ import print_function # not necessary if using Python 3.x

for i in range(5, 0, -1):
    print(i, end="")
    time.sleep(1)
    if i == 1:
        print(' Blastoff!')

The above will print:

54321 Blastoff!
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Or, remove the condition test and unindent the `print 'Blastoff'` line 2 positions (8 spaces) – KevinDTimm Nov 27 '13 at 22:03
  • The OP wanted to print them without the spaces – Aswin Murugesh Nov 28 '13 at 15:14
  • @AswinMurugesh that's not how I understand the question, OP wanted to print them in the same line, he stated it explicitly, and that's all. In the question he simply didn't put the spaces the second time. Please reconsider the downvote. – Óscar López Nov 28 '13 at 15:24
  • `so what I want to do is instead of printing it 5 4 3 2 1 Blastoff I want 54321 Blastoff` Is what the question states – Aswin Murugesh Nov 28 '13 at 15:27
  • @AswinMurugesh "on the same line" it says next, don't quote out of context. Anyway, I edited my question to consider that case, too – Óscar López Nov 28 '13 at 15:32
  • Sorry to say this, but this is an obvious solution, but not the OP's wish. He wants 5,4,3,2,1 to be printed one per second. Please look at the question carefully – Aswin Murugesh Nov 28 '13 at 15:33
  • Again, I see `on the same line`. But clearly looking at the question, `instead of printing it 5 4 3 2 1`. So it shows that he doesn't want that – Aswin Murugesh Nov 28 '13 at 15:35
  • @AswinMurugesh ugh, I forgot the timer. Ok, ok, I edited again :( – Óscar López Nov 28 '13 at 15:40
2

In Python 3 one should pass end="" to the print function.

Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44
1

The following codes works in Python 2 and Python 3.
But for Python 3, see the good answer of Ramchandra Apte.

from sys import stdout
from time import sleep

for i in xrange(5,0,-1):
    stdout.write(str(i))
    sleep(1)
stdout.write(' Blastoff')

The str(i) is necessary in order thatthis code executes in a command line window. In an EDIT shell window , it can be written stdout.write(i) but the '\b' signal doesn't move the cursor back, it appears a square in place of it.

.

By the way, try the following code to see what happens. The '\b' triggers the cursor to move back of one position, replacing the preceding character, so each character is written just at the place of the one the preceded it in time. That works only in a command line window, not in an EDIT shell window.

from sys import stdout
from time import sleep

for i in xrange(5,0,-1):
    stdout.write(str(i))
    sleep(1)
    stdout.write('\b')
stdout.write('Blastoff')


raw_input('\n\npause')

Complexifying (still to execute in a command line window):

from sys import stdout
from time import sleep

tu = (' End in 24 seconds',
      ' It remains 20 seconds',
      ' Still only 16 seconds',
      ' Do you realize ? : 12 seconds left !',
      ' !!!! Hey ONLY 8 seconds !!')
for s in tu:
    stdout.write('*')
    sleep(1)
    stdout.write(s)
    sleep(2)
    stdout.write(len(s)*'\b' + len(s)*' ' + len(s)*'\b')
    sleep(1)

stdout.write(len(tu)*'\b' + len(tu)*'!' + ' ')
n = 4
for x in xrange(n,0,-1):
    stdout.write('\b!'+str(x))
    sleep(1)

stdout.write(len(tu)*'\b' + n*'\b' + '\b')
stdout.write('       # !! BLASTOUT !! #\n')
sleep(0.8)
stdout.write('      ## !! BLASTOUT !! ##\n')
sleep(0.8)
stdout.write('     ### !! BLASTOUT !! ##\n')
sleep(0.8)
stdout.write('    #### !! BLASTOUT !! ####\n')
sleep(3)
eyquem
  • 26,771
  • 7
  • 38
  • 46