How to print coutdown in python on same line? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to print coutdown in python on same line?

I am trying to create a quiz in python for that i have created a 10 second countdown but it prints every second on next line i want it to overwright previous line any suggestions??

12th Aug 2018, 8:09 AM
Neeraj kumar
Neeraj kumar - avatar
5 Answers
+ 9
Store whatever value that you want to print into a variable as a string, and use a for-loop. After each second, add the number (as a string) to the string, and reprint it.
12th Aug 2018, 8:10 AM
Fox
Fox - avatar
+ 5
You cant do that here as you code is executed on sololearns servers and the result returned and printed to screen you wont be able to see it refresh replacing the previous number with a new one.
12th Aug 2018, 8:14 AM
D_Stark
D_Stark - avatar
+ 1
can anybody share code for this??
12th Aug 2018, 8:22 AM
Neeraj kumar
Neeraj kumar - avatar
+ 1
import os import time timer = 10 while timer > 0: os.system("cls") print(timer) timer -= 1 time.sleep(1) thats just for the timer. You might want to look into threading and getch to allocate the timer "outside" the quiz. since timer will refresh the shell every second you need single keypress listener for every question in the quiz to check if the person answering presses the correct key within the period of less than a second
12th Aug 2018, 8:38 AM
Markus Kaleton
Markus Kaleton - avatar
0
if you are using shell to run this you need to import threading getch from msvcrt time and you need to make it as a multiple choice quiz. also you could import os and do os.system("cls"), to make the countdown stay in same place and just count numbers down.
12th Aug 2018, 8:21 AM
Markus Kaleton
Markus Kaleton - avatar