Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73851
| Date | 2014-07-02 21:02 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: OOP with MyTime |
| References | <7020a5d8-96a9-4fa4-8e69-4c7593dedee3@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11418.1404331330.18130.python-list@python.org> (permalink) |
On 2014-07-02 20:20, kjakupak@gmail.com wrote:
> I'm trying to write a boolean function that takes two Mytime objects, t1 and t2 as arguments, and returns True if the object falls inbetween the two times.
>
> This is a question from the How to Think Like a Computer Scientist book, and I need help.
>
> What I've gotten so far:
>
> class MyTime:
> def __init__(self, hrs=0, mins=0, secs=0):
> self.hours = hrs
> self.minutes = mins
> self.seconds = secs
> def between(t1, t2):
> if float(t1 <= t3) and float(t3 < t2):
> return True
> else:
> return False
>
> I just don't understand how to make a function that uses MyTime objects into the boolean function? Any help would be great.
>
If you want 'between' to be an instance method of the MyTime class, it
needs 'self' as well as the 2 arguments 't1' and 't2'.
You can then compare the hours, minutes and seconds of self against
those of t1 and t2:
def between(self, t1, t2):
return (t1.hours, t1.minutes, t1.seconds) <= (self.hours,
self.minutes, self.seconds) and (self.hours, self.minutes, self.seconds)
<= (t2.hours, t2.minutes, t2.seconds)
That could be shortened further using chained comparisons.
Note that the code assumes that the times t1 and t2 are ordered, i.e.
that time t1 is not later/greater than time t2.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
OOP with MyTime kjakupak@gmail.com - 2014-07-02 12:20 -0700
Re: OOP with MyTime Akira Li <4kir4.1i@gmail.com> - 2014-07-02 23:45 +0400
Re: OOP with MyTime MRAB <python@mrabarnett.plus.com> - 2014-07-02 21:02 +0100
Re: OOP with MyTime kjakupak@gmail.com - 2014-07-03 05:51 -0700
Re: OOP with MyTime Chris Angelico <rosuav@gmail.com> - 2014-07-03 23:01 +1000
Re: OOP with MyTime kjakupak@gmail.com - 2014-07-03 06:08 -0700
Re: OOP with MyTime Chris Angelico <rosuav@gmail.com> - 2014-07-03 23:17 +1000
Re: OOP with MyTime MRAB <python@mrabarnett.plus.com> - 2014-07-03 14:11 +0100
Re: OOP with MyTime kjakupak@gmail.com - 2014-07-03 08:21 -0700
Re: OOP with MyTime Chris Angelico <rosuav@gmail.com> - 2014-07-04 01:35 +1000
Re: OOP with MyTime Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-03 17:23 +0100
csiph-web