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.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  zfill prints extra et the end of a var tester_V 4 897 Mar-24-2023, 06:59 PM
Last Post: tester_V
  variable prints without being declared. ClockPillow 2 1,804 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  [solved] unexpected character after line continuation character paul18fr 4 3,393 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  Output prints Account.id at the end? LastStopDEVS 5 2,764 Dec-19-2020, 05:59 AM
Last Post: buran
  Try/Exept prints only ones tester_V 11 3,866 Nov-03-2020, 02:38 AM
Last Post: tester_V
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,166 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 2,735 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  For loop prints strane values? colt 2 2,189 Sep-22-2019, 02:01 AM
Last Post: ichabod801
  Replace changing string including uppercase character with lowercase character silfer 11 6,192 Mar-25-2019, 12:54 PM
Last Post: silfer
  can you understand why this code prints None? arcbal 2 2,747 Mar-13-2019, 02:57 AM
Last Post: arcbal

Forum Jump:

User Panel Messages

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