-3

I've written a class called "Time" that does the following: It takes the values of hours, minutes and seconds from the user and prints it in the form hh:mm:ss. If the user does not enter any arguments through the time module, the time object is taken and the values of hours, minutes and seconds are stored in it.

class Time:
    import time
    obj_time = time.localtime()
    def __init__(self, hour=obj_time.tm_hour, minute=obj_time.tm_min, second=obj_time.tm_sec):
        self.hour=hour
        self.minute=minute
        self.second=second
        print('Time is: {}:{}:{}'.format(hour,minute,second))

    def String_time():
        #(my problem)???
Time.__init__(Time)

But I want to write another function called "String_time" for this class, so that by taking the values of hours, minutes and seconds, it will print them in the form of hh:mm:ss But in such a way that if the user enters the hour "5" for example, it will be changed to "05" and then printed. My problem is that the hour, minute and second values are defined in the __init__ method, how can I use them in the string_time function?

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
Babilo
  • 1

2 Answers2

0

You can access them passing the self implicit argument to the method.

def string_time(self) -> str:
    print(f"Here you can access {self.time}")
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
0

Hope this helps

import time
class Time:
    
    obj_time = time.localtime()
    def __init__(self, hour=obj_time.tm_hour, minute=obj_time.tm_min, second=obj_time.tm_sec):
        self.hour=hour
        self.minute=minute
        self.second=second
        print('Time is: {}:{}:{}'.format(hour,minute,second))

    def String_time(self):
        print("Printing from String_time")
        print(self.hour)
        print(self.minute)
        print(self.second)
        
t = Time()
t.String_time()

You can access the class variables this way from String_time() function and it should show output as you want

Musabbir Arrafi
  • 744
  • 4
  • 18