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)
time_addfunction.time_sum.hoursshould betime_sum.hour. Andtime_sum.minutesshould betime_sum.minute