0

So say I have this:

import replit
import time

print("Text")

i = 0
def counter():
  global i
  while True:
    print(i)
    i +=1
    time.sleep(1)
    replit.clear()

counter()

Every second it clears the entire console and prints i out again, but say I want to keep 'Text' on the screen, how would I delete/edit that line? I have tried \r but if I type it breaks, any ideas?

  • you could use ansi escape codes to do this like `print("\u001b[A\u001b[2K")` here `\u001b[A` goes back up 1 line and `\u001b[2K` clears the line the cursor is on (you can find more codes for just moving cursor left right or anything on https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences) – DavinderJolly Jun 08 '22 at 14:13
  • Maybe this question helps you: https://stackoverflow.com/questions/36520120/overwriting-clearing-previous-console-line – Palinuro Jun 08 '22 at 14:16
  • @DavinderJolly that almost works, but it just moves the text down, instead of keeping it in one place – StinkingMerm Jun 09 '22 at 07:25
  • check out the link i gave at the end there it shows how to move your cursor however you want using ansii codes so you could get the bahavior you want but also keep in mind that this may not work on default powershell and cmd windows in window (will work on windows terminal) without doing `os.system('')` i don't really know why but on windows doing that makes it work on all windows also check out the link Palinuro sent it shows how to do it with just `\r` (carrige return) which takes your cursot back to start of line – DavinderJolly Jun 09 '22 at 08:15
  • That worked! Thanks a lot! I just changed A to 2A – StinkingMerm Jun 09 '22 at 08:37

0 Answers0