0

I tested the following code with an online Python 3.x compiler but want a similar one to work on my 2.4.3 compiler:

import sys, time print('I am about to show you something _\b', end='') sys.stdout.flush() # Make sure above string gets printed in time time.sleep(2) print('THIS!') 

How can I make a similar code work for Python 2.4.3?

5
  • Did you try this code in Python 2.4? Commented Jan 12, 2018 at 3:08
  • print is not function until python 3 Commented Jan 12, 2018 at 3:09
  • 3
    May I suggest if you are learning Python, you should upgrade to Python3. Python2 will be at end-of-life soon Commented Jan 12, 2018 at 3:10
  • i have tried other versions of this code to try to make it work but it generates a syntax error. Commented Jan 12, 2018 at 3:11
  • 2
    Do you really need to keep using Python 2.4? That release is coming up on 12 years old, and has been unsupported for a very long time. It has security bugs that will not be fixed (they are fixed in newer releases). Commented Jan 12, 2018 at 3:14

1 Answer 1

2

Use the print statement with trailing comma to suppress the newline:

import time import sys print 'I am about to show you something _\b', sys.stdout.flush() # Make sure above string gets printed in time time.sleep(2) print 'THIS!' 

Alternatively, write to sys.stdout directly:

sys.stdout.write('I am about to show you something ') sys.stdout.flush() # Make sure above string gets printed in time time.sleep(2) sys.stdout.write('THIS!') 

This works in Python 2 and 3 alike.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.