1

I am trying to print random numbers using random , but when I try to print the output in one line using end= " " the output doesnt show anything until I break the program.

import random
import time
while True:
    x = random.randint(1,6)
    print(x, end=" ")
    time.sleep(1)

The out put is like this after I interrupt :

C1 2 3 5 5 4 5 4 1 ---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Fenomatik
  • 457
  • 2
  • 8
  • 22

2 Answers2

5

You can disable buffering by passing flush=True to print function (in python3)

print(x, end=" ", flush=True)
Mikhail M.
  • 5,588
  • 3
  • 23
  • 31
0

The easiest way of doing it as follows:

print('Hello World!', flush=True , end = " ")
Fenomatik
  • 457
  • 2
  • 8
  • 22