Jan-07-2018, 10:59 AM
Hi everyone,
I'm quite new to Python programming, which is why this thread is located in the Homework section: it's not because of a teacher that I'm restricted in my use of syntax, it's because of my basic knowledge.
I'm trying to program a clock to learn how to make a piece of code overwrite the last line I printed out to the display, to simulate a clock. I've been doing some research (the internet is stuffed with threads about this, I'm sorry), but everyone justs keeps going on about this infamous '\r' - thing, which is the return carriage, I suppose, and which should erase the last line and print the next line in place of the last line. On my computer, that is not what it does though: it just prints the next line right besides the previous line, resulting in some sort of junk-line of concatenated print-lines. So I found out about this 'flush'-thing that should clear your last line, but instead of clearing, it just does the exact same as if it weren't there (even if I set is to 'False'). Is this related to the type of computer I have? I found it was device-specific, but I found that quite strange.
Here's the failing code:
>>> %Run joke.py
Sun Jan 7 11:40:47 2018Sun Jan 7 11:40:48 2018Sun Jan 7 11:40:49 2018Sun Jan 7 11:40:50 2018Sun Jan 7 11:40:51 2018Sun Jan 7 11:40:52 2018Sun Jan 7 11:40:53 2018Sun Jan 7 11:40:54 2018Sun Jan 7 11:40:55 2018Sun Jan 7 11:40:56 2018Sun Jan 7 11:40:57 2018Sun Jan 7 11:40:58 2018Sun Jan 7 11:40:59 2018Sun Jan 7 11:41:00 2018Sun Jan 7 11:41:01 2018Sun Jan 7 11:41:02 2018Sun Jan 7 11:41:03 2018Sun Jan 7 11:41:04 2018Sun Jan 7 11:41:05 2018Sun Jan 7 11:41:06 2018
I'm quite new to Python programming, which is why this thread is located in the Homework section: it's not because of a teacher that I'm restricted in my use of syntax, it's because of my basic knowledge.
I'm trying to program a clock to learn how to make a piece of code overwrite the last line I printed out to the display, to simulate a clock. I've been doing some research (the internet is stuffed with threads about this, I'm sorry), but everyone justs keeps going on about this infamous '\r' - thing, which is the return carriage, I suppose, and which should erase the last line and print the next line in place of the last line. On my computer, that is not what it does though: it just prints the next line right besides the previous line, resulting in some sort of junk-line of concatenated print-lines. So I found out about this 'flush'-thing that should clear your last line, but instead of clearing, it just does the exact same as if it weren't there (even if I set is to 'False'). Is this related to the type of computer I have? I found it was device-specific, but I found that quite strange.
Here's the failing code:
import time x=0 for x in range(0,20): print(time.ctime(), end="\r", flush=True) time.sleep(1)And here's what it results:
>>> %Run joke.py
Sun Jan 7 11:40:47 2018Sun Jan 7 11:40:48 2018Sun Jan 7 11:40:49 2018Sun Jan 7 11:40:50 2018Sun Jan 7 11:40:51 2018Sun Jan 7 11:40:52 2018Sun Jan 7 11:40:53 2018Sun Jan 7 11:40:54 2018Sun Jan 7 11:40:55 2018Sun Jan 7 11:40:56 2018Sun Jan 7 11:40:57 2018Sun Jan 7 11:40:58 2018Sun Jan 7 11:40:59 2018Sun Jan 7 11:41:00 2018Sun Jan 7 11:41:01 2018Sun Jan 7 11:41:02 2018Sun Jan 7 11:41:03 2018Sun Jan 7 11:41:04 2018Sun Jan 7 11:41:05 2018Sun Jan 7 11:41:06 2018