0

So, for example, I could have a stopwatch that seems to have the units changing rather than repeating underneath every time they change. I have heard of many ways to do this, like \r and sys, but these do not work on my windows python 3.6.5.(this code that doesn't work is shows below:

def clock():
import time
for i in range(60):
    time.sleep(1)
    print(i+1,end="\r seconds")
AuctortiA
  • 146
  • 7
  • 1
    Please post the code that didn't work to help others to identify problem. – gzc Apr 11 '17 at 16:45
  • This question [text-progress-bar-in-the-console](http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console) should help you. – gzc Apr 11 '17 at 16:46
  • Possible duplicate of [Python - Remove and Replace Printed items](http://stackoverflow.com/questions/5290994/python-remove-and-replace-printed-items) – gzc Apr 11 '17 at 16:50
  • The problem in the other question is identical, but I do not get any of the desired outcomes when using the answers referenced there on my version of python as stated in my question. – AuctortiA Apr 11 '17 at 17:02
  • In a generic function to print reusing the current line, first clear the line, e.g. `print('\r', ' ' * (cols - 1), sep='', end='\r', flush=True)`. Then you can follow that with a generic `print(*args, **kwds)`, assuming you've set default values `kwds['end'] = ''` and `kwds['flush'] = True`. This intentionally doesn't use `end='\r'` on the output print, because a blinking cursor on the first character looks bad. You can get the column count from `cols, lines = shutil.get_terminal_size()`. Unfortunately you have to subtract a column; the console auto-feeds at EOL (configurable in Windows 10). – Eryk Sun Apr 11 '17 at 22:53
  • So, how would I write that in code to, for example, create a clock that updates the print line rather than prints it on a new line. – AuctortiA Apr 12 '17 at 12:40
  • Also what does any of that mean, I only know the basics of Python and when I tried to make this code work Python has no clue what any of it meant. – AuctortiA Apr 12 '17 at 12:47

2 Answers2

1

You can do it easy with os

import os
def osClearScreen()
    os.system('cls')
def clock():
    for i in range(60):
        time.sleep(1)
        print(i+1)
        osClearScreen()

Hope this helped

0

As always kids, remember to print('...', end='\r', flush=True) to make it happen.

user2722968
  • 13,636
  • 2
  • 46
  • 67