calling a method inside a class-Python

Viewed 36246
class Time:

    def __init__(self,x,y,z):
        self.hour=x
        self.minute=y
        self.second=z

    def __str__(self):
        return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)

    def time_to_int(time):
        minutes=time.hour*60+time.minute
        seconds=minutes*60+time.second
        return seconds

    def int_to_time(seconds):
        time=Time()
        minutes,time.second=divmod(seconds,60)
        time.hour,time.minute=divmod(minutes,60)
        return time

    def add_time(t1,t2):
        seconds=time_to_int(t1)+time_to_int(t2)
        return int_to_time(seconds)

start=Time(9,45,00)
running=Time(1,35,00)
done=add_time(start,running)
print(done)

I am new to python and i've been doing some practice lately.I came across a question and i've written the code for the same.But I am repeatedly getting an error: "add_time is not defined". I tried defining a main() method but then it doesn't print anything.Please help.

5 Answers
class Employee:
    def __init__(self):
        self.wage = 0
        self.hours_worked = 0

    def calculate_pay(self):
        return self.wage * self.hours_worked

alice = Employee()
alice.wage = 9.25
alice.hours_worked = 35
print('Alice:\n Net pay: {:.2f}'.format(alice.calculate_pay()))

barbara = Employee()
barbara.wage = 11.50
barbara.hours_worked = 20
print('Barbara:\n Net pay: {:.2f}'.format(barbara.calculate_pay()))
Related