Python Forum
loop only prints last character.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop only prints last character.
#1
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
Reply
#2
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)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 321 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  Try/Exept prints only ones tester_V 11 844 Nov-03-2020, 02:38 AM
Last Post: tester_V
  SyntaxError: unexpected character after line continuation character siteshkumar 2 936 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 1,032 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  For loop prints strane values? colt 2 897 Sep-22-2019, 02:01 AM
Last Post: ichabod801
  Replace changing string including uppercase character with lowercase character silfer 11 2,604 Mar-25-2019, 12:54 PM
Last Post: silfer
  What for a file that prints nothing sylas 1 1,014 Sep-12-2018, 01:18 PM
Last Post: gruntfutuk
  msvcrt.getch prints twice inside loop ReallyBigTeeth 5 4,000 Sep-04-2018, 06:54 AM
Last Post: ReallyBigTeeth
  phrase loop and character comparision shiro26 6 1,823 Jul-06-2018, 02:03 AM
Last Post: shiro26
  SyntaxError: unexpected character after line continuation character Saka 2 16,531 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020