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)

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Try/Exept prints only ones tester_V 11 287 Nov-03-2020, 02:38 AM
Last Post: tester_V
  Pip prints huge error when installing p5 hayden2s 1 296 Aug-08-2020, 02:30 PM
Last Post: snippsat
  SyntaxError: unexpected character after line continuation character siteshkumar 2 350 Jul-13-2020, 07:05 PM
Last Post: snippsat
  python display with '\\' when prints with key-value in dictionary maiya 12 1,071 May-30-2020, 05:56 PM
Last Post: maiya
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 588 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  For loop prints strane values? colt 2 526 Sep-22-2019, 02:01 AM
Last Post: ichabod801
  Replace changing string including uppercase character with lowercase character silfer 11 1,756 Mar-25-2019, 12:54 PM
Last Post: silfer
  can you understand why this code prints None? arcbal 2 807 Mar-13-2019, 02:57 AM
Last Post: arcbal
  What for a file that prints nothing sylas 1 735 Sep-12-2018, 01:18 PM
Last Post: gruntfutuk
  msvcrt.getch prints twice inside loop ReallyBigTeeth 5 3,063 Sep-04-2018, 06:54 AM
Last Post: ReallyBigTeeth

Forum Jump:

User Panel Messages

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