[Python] Pyyhon f-string having Syntax error for literal ':' in between [closed]

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am facing an error while executing 2nd object created by eval(). I'm getting an error of invalid syntax for ':' while printing the 2nd object - timer_repr_object. Code execution result printout:

    1st Timer- 23:59: 59

    2nd Timer- 00:00: 00

    Traceback (most recent call last):
    File "c:\Users\kanch\OneDrive\Documents\python\workspace\python1st\DisplayClock24hour.py", line 54, in <module>
    timer_repr = eval(repr(timer))

    File "<string>", line 1

    **Timer- 23:59: 59 (error in 1st ':')**


    SyntaxError: invalid syntax


    Below is the code.

    def two_digits(val):
    s = str(val)
    if len(s) == 1:
    s = '0' + s
    return s

    class Timer:
    def __init__(self, hours=0, minutes=0, seconds=0):
    self.__hours = hours
    self.__minutes = minutes
    self.__seconds = seconds

    def __repr__(self):
    return f'Timer- {two_digits(self.__hours)}:{two_digits(self.__minutes)}: {two_digits(self.__seconds)}'

    def next_second(self):
    self.__seconds += 1
    if self.__seconds > 59:
    self.__seconds = 0
    self.__minutes += 1
    if self.__minutes > 59:
    self.__minutes = 0
    self.__hours += 1
    if self.__hours > 23:
    self.__hours = 0

    def prev_second(self):
    self.__seconds -= 1
    if self.__seconds < 0:
    self.__seconds = 59
    self.__minutes -= 1
    if self.__minutes < 0:
    self.__minutes = 59
    self.__hours -= 1
    if self.__hours < 0:
    self.__hours = 23

    timer = Timer(23, 59, 59)
    print('1st ', timer, end ='\n')
    timer.next_second()
    print('2nd ', timer , end='\n')
    timer.prev_second()
    timer_repr_object = eval(repr(timer))
    print('3rd ')
    print(timer_repr_object)

    Continue reading...

Compartilhe esta Página