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
  • 225,348
  • 35
  • 301
  • 374
2

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

Ramchandra Apte
  • 3,975
  • 2
  • 23
  • 43
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
  • 25,715
  • 6
  • 36
  • 44