I m在比较一类变量方面存在问题
当我打电话<条码>(t2.between(t1, t3)条码>。
这是我发现的错误:
Traceback (most recent call last):
File "e:CS-OOP-Fall 2023Classtest1.py", line 18, in <module>
print(t2.between(t1, t3)) # is t2 between t1 and t3 as a method
^^^^^^^^^^^^^^^^^^
File "e:CS-OOP-Fall 2023MyTime.py", line 81, in between
return (t1 < self < t3)
^^^^^^^^^^^^^^
TypeError: < not supported between instances of MyTime and MyTime
这里是:
class MyTime:
def __init__(self, hrs=0, mins=0, secs=0):
""" Create a new MyTime object initialized to hrs, mins, secs.
The values of mins and secs may be outside the range 0-59,
but the resulting MyTime object will be normalized.
"""
# Calculate total seconds to represent
totalsecs = hrs*3600 + mins*60 + secs
self.hours = totalsecs // 3600 # Split in h, m, s
leftoversecs = totalsecs % 3600
self.minutes = leftoversecs // 60
self.seconds = leftoversecs % 60
def __str__(self):
# 0 - hours, 1 - minutes, 2 - seconds
return ("{0}:{1}:{2}".format(self.hours, self.minutes, self.seconds))
def add_time(self, t2):
h = self.hours + t2.hours
m = self.minutes + t2.minutes
s = self.seconds + t2.seconds
while s >= 60:
s -= 60
m += 1
while m >= 60:
m -= 60
h += 1
sum_t = MyTime(h, m, s)
return sum_t
def increment(self, seconds):
self.seconds += seconds
while self.seconds >= 60:
self.seconds -= 60
self.minutes += 1
while self.minutes >= 60:
self.minutes -= 60
self.hours += 1
def to_seconds(self):
""" Return the number of seconds represented
by this instance
"""
return self.hours * 3600 + self.minutes * 60 + self.seconds
def after(self, t2, t3):
""" Return True if I am strictly greater than both t2 and t3
"""
if self.hours > t2.hours and self.hours > t3.hours:
return True
if self.hours < t2.hours or self.hours < t3.hours:
return False
if self.minutes > t2.minutes and self.minutes > t3.minutes:
return True
if self.minutes < t2.minutes or self.minutes < t3.minutes:
return False
if self.seconds > t2.seconds and self.seconds > t3.seconds:
return True
if self.seconds < t2.seconds or self.seconds < t3.seconds:
return False
return False
def between(self, t1, t3):
"""Checks if one of the specified times falls between
another the other times with True or False as a indicator
"""
# this will cover both cases
return (t1 < self < t3)
我对两件事进行了审判,但没有一件事奏效,我对变数的其他部分进行了审判,但似乎没有做正确的工作。
最后,我想说的是<代码>True或False
,其依据是你选择检查的时间,但我想看一下其他工作是否首先奏效。