0

I am new to python 2.7 and just getting into classes. I wrote the block of code below for section 13.2 from "How Think Like a Computer Scientist: Learning With Python"

When I call the print_time function to print the returned attributes from the add_time function I get the following error:

Traceback (most recent call last):
File "13.py", line 49, in <module>
print_time(done_time)
File "13.py", line 10, in print_time
print time.hour,':',time.minute
AttributeError: Time instance has no attribute 'hour'

Why is this? Do I need to explicitly tell python which attributes in the done_time class to print?

class Time:
    pass

def print_time(time):
    print time.hour,':',time.minute

def time_add(t1, t2):
    time_sum = Time()
    time_sum.hours = t1.hour + t2.hour
    time_sum.minutes = t1.minute + t2.minute
    return time_sum

current_time = Time()
current_time.hour = 9
current_time.minute = 00

duration = Time()
duration.hour = 2
duration.minute = 30

done_time = time_add(current_time, duration)
print_time(done_time)
4
  • Your indentations are way off. Make sure to put the class functions within the class. Edit: my mistake... Didn't fully read the question ;) Commented Nov 4, 2016 at 1:39
  • 2
    @not_a_robot No, the code is properly indented and this code reproduces the exact error OP states. The error has to do with calling the wrong attribute names. Commented Nov 4, 2016 at 1:40
  • @not_a_robot What do you mean? His indention is fine. How do you know he wanted those functions in his class? Looks more like he was creating a skeleton class to me. Commented Nov 4, 2016 at 1:40
  • You have a typo in your time_add function. time_sum.hours should be time_sum.hour. And time_sum.minutes should be time_sum.minute Commented Nov 4, 2016 at 1:44

1 Answer 1

1

The overall structure of the code is a bit weird. I'm assuming that maybe this is deliberate per the book you are following to help teach towards a proper design for this code down the road? Either way, the problem is actually because you are mixing your hour and hours attributes (minute and minutes for that matter as well). I'm not sure if this is code you created on your own, but, starting here:

done_time = time_add(current_time, duration)

What is happening is that your time_add function is returning a Time object with attributes, hours and minutes.

The following line, you do:

print_time(done_time)

Which, takes you to your print_time function, which then tries to reference the hour and minute attribute, which you clearly do not have in done_time. Based on your previous function, you, as stated, assigned hours and minutes. To immediately fix your problem, your print_time should be referencing hours and minutes:

def print_time(time):
    print time.hours,':',time.minutes
Sign up to request clarification or add additional context in comments.

1 Comment

This clarified and fixed the problem. New to programming, thank you!

Your Answer

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.