Python Forum

Full Version: loop only prints last character.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want this to reprint and replace the time on the same line but its only printing the last character.
If I take out the end="\r" it prints the time vertically.

from datetime import datetime
import time


def get_time():
    now = datetime.now()
    current_time = now.strftime("%H:%M")
    return current_time

while True:
    for i in get_time():
        print(i, end="\r") # Only prints last character of get_time().
    time.sleep(60)
Result:
6
Try this

from datetime import datetime
import time
 
 
def get_time():
    now = datetime.now()
    current_time = now.strftime("%H:%M")
    return current_time
 
while True:
    print(get_time(), end="\r") # Only prints last character of get_time().
    time.sleep(60)