10
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.

asked Jun 1, 2017 at 7:01
1
  • 1
    Please fix your indentation. Are time_to_int and so on supposed to be standalone functions, or methods of the class? If the former, they need to be dedented outside the class definition. Commented Jun 1, 2017 at 7:04

5 Answers 5

10

You haven't created an object to the above class.

Any function/method inside a class can only be accessed by an object of that class .For more information on the fundamentals of Object Oriented Programming, please check this page.

Meanwhile for this to work, define your class in the following way :

class Time:
def __init__(self,x=None,y=None,z=None):
 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)

and outside the class block, write the following lines :

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

I however suggest you to write the add_time function outside the class because you are passing the objects to the class as the parameters to the function within the same class and it is considered as a bad design in object oriented programming. Hope it helps. Cheers!

answered Jun 1, 2017 at 7:08
Sign up to request clarification or add additional context in comments.

9 Comments

one question.Now according to your code all the methods standalone?right?
No. start and running are objects of your class time. To access the add_time method of the class Time, you need another object and hence I have created a TimeObject just to access the add_time method. However I suggest you make the add_time method independant and rest can be within the class.
And here you've created a TimeObject but it has to be initialised,otherwise it would give an error.
Sorry I missed that, now I have edited so I can initialize it without having to pass it any parameter.
thanks @crazyglasses.I would have upvoted your ans but i've a reputation below 15.
|
3

Hii Mathers25,
I solve your problem try this below code to get the best output,

class TimeClass:
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(self,time):
 minutes = (time.hour * 60) + time.minute
 seconds = (minutes * 60) + time.second
 return seconds
def int_to_time(self,seconds):
 time = TimeClass(0,0,0)
 minutes,time.second=divmod(seconds,60)
 time.hour,time.minute=divmod(minutes,60)
 return time
def add_time(self,t1,t2):
 seconds = self.time_to_int(t1) + self.time_to_int(t2)
 # Call method int_to_time() using self keyword.
 return self.int_to_time(seconds)
# First time object create that time set value is 0 of hour,minute and second
TimeObject = TimeClass(0,0,0)
# After create second object
start=TimeClass(9,45,00)
# After create thired Object
running=TimeClass(1,35,00)
# Store the value which return by add_time() 
done = TimeObject.add_time(start,running)
# Display the value of done variable
print(done)
answered Jun 1, 2017 at 9:43

Comments

1

This works fine for me as long as you specified 3 args in your constructor

def int_to_time(seconds):
 time=Time(0,0,0) # just set your 3 positionals args here
 minutes,time.second=divmod(seconds,60)
 time.hour,time.minute=divmod(minutes,60)
 return time

Another way to avoid it could be:

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

If you want to add your functions to your class (such as time_to_int, int_to_time or even add_time) then you will need to indent with one more level of 4 spaces and add self to your method parameters

answered Jun 1, 2017 at 7:16

1 Comment

thanks! I got it know.I was trying to call a class method without using an object.
1
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()))
answered Feb 24, 2022 at 19:09

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

Works for me:

class C:
 def f(a, b):
 return a + b
 x = f(1,2)
print(C.x)

but you should not do such things. Code in class-level is executing when class is "creating", usually you want static methods or class methods (decorated with @staticmethod or @classmethod) and execute code in some function/instantiated class. Also you can execute it on top (module) level if this is the simple script. Your snippet is "bad practice": class level (i'm talking about indentation) is for declarations, not for execution of something. On class-level is normal to execute code which is analogue of C macros: for example, to call decorator, to transform some method/attribute/etc - static things which are "pure" functions!

answered Jun 1, 2017 at 7:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.