0

So i have a code of a timer and When a person puts in the number i just want the timer to start and inputted number to not be visible.Code is something like

s=int(input("enter time in sec"))
countdown(s)

so the output is :

enter time in sec 5
0:4
0:3
0:2
0:1
0:0
time up

What i want is to first remove "enter time in sec 5" then when 0:4 prints i want to print 0:3 in its place not below it.

I tried Python deleting input line and copy pasted this on the code like so

s = int(input("enter time in sec "))
print ('\033[1A\033[K')
countdown(s)

and nothing seemed to happen, don't if im wrong in the implementation or it didn't work.

Edit:-

Tried both

os.system('cls')

and

print ('\033[1A\033[K')

neither seemed to work

my code,

 def time_format(inp):
          import os
          m,s=divmod(inp,60)
          #os.system('cls')
          print ('\033[1A\033[K')
          ...code for printing time below...

Edit:- im on windows and am using Idle.
neither of the two work

Pratyush Arora
  • 137
  • 1
  • 11

3 Answers3

0

See accepted answer of: How to clear the interpreter console?

os.system('cls') works on Windows, while os.system('clear') works on Linux

dovregubben
  • 364
  • 2
  • 16
0

You didn't specify what OS you use but if you target Linux it could be:

#!/usr/bin/env python3

import time


def countdown(seconds):
    for i in range(seconds, -1, -1):
        # move to the beginning of the line and remove line
        print("\r\033[K", end='', flush=True)
        print(f"\r{i}", end='', flush=True)
        time.sleep(1)
    print("\nTime's up!")


s = int(input("enter time in sec: "))
# move one line up
print("\033[1A", end='', flush=True)
countdown(s)

It works like that: enter image description here

Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
0

Got a pop saying I shouldn't delete answered question therefore answering it myself. I believe both the

print("\033[1A", end='', flush=True)

and

os.system('cls')

works. The issue is that there is no option/method to do it on IDLE. because i tried both the methods work if i double click and run the file as is but none work on IDLE

Pratyush Arora
  • 137
  • 1
  • 11