I'm attempting to write a program that involves the use of a coin flip, heads or tails, but so that it will print 'heads' then be replaced by 'tails' and continue doing this until it decides on an answer.
At the moment, when I run the program, it prints the 'heads' or 'tails' on the next line every time. This happens on both Idle and Terminal.
I've tried using carriage return (\r), backspace (\b) and sys.stdout.write() and .flush() but neither are working, it just keeps printing on the next line.
Is there either another way of erasing what's been printed or is there other software I can use? Here is my code:
import time
import random
offset = random.randint(0,1)
for i in range (0, 20+offset):
if i % 2 == 0:
print("Heads")
else:
print("Tails")
print("\r")
time.sleep(0.1)