Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'class,': 0.07; 'great.': 0.07; 'arguments': 0.09; 'arguments,': 0.09; 'falls': 0.09; 'objects,': 0.09; 'def': 0.12; 'boolean': 0.16; 'chained': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'function?': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'scientist': 0.16; 'subject:OOP': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'help.': 0.21; 'header:User- Agent:1': 0.23; 'hours,': 0.24; 'question': 0.24; "i've": 0.25; 'compare': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; 'code': 0.31; 'assumes': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'subject:with': 0.35; 'objects': 0.35; 'false': 0.36; 'i.e.': 0.36; 'method': 0.36; 'seconds': 0.37; 'two': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'further': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'minutes': 0.67; 'book,': 0.68; 'gotten': 0.74; "'between'": 0.84; 'mins': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OZcWD3jY c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=kDp_w4HpGs9OZO7lXYQA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Wed, 02 Jul 2014 21:02:00 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: OOP with MyTime References: <7020a5d8-96a9-4fa4-8e69-4c7593dedee3@googlegroups.com> In-Reply-To: <7020a5d8-96a9-4fa4-8e69-4c7593dedee3@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404331330 news.xs4all.nl 2924 [2001:888:2000:d::a6]:46094 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73851 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.