Re: Strange error in Python code

Hi,
Lol it seems so obvious now. Forgetting to put self.variable can be
detrimental.
I got all of that fixed and am still getting an error about no attribute
called now, but it is there, I double checked.  Here's the revised code:
import time
class countPresses:
    def __init__(self, now = time.time(), clicks = 1):
        pass

    def clickCount(self):
        if time.time() <= self.now + 0.75:
            self.clicks += 1
        self.now = time.time()
        if self.clicks > 2: self.clicks = 1
        return self.clicks

test = countPresses()
print str(test.clickCount())
print str(test.clickCount())



-- 
Thoughts of a Dragon:
http://www.stormdragon.us/
What color dragon are you?
http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/



On Sun, 2009-12-13 at 07:30 -0800, R Dinger wrote:

>  
> 
> Your calls to clickCount do not have parens e.g. clickCount(), which
> is required for a function call.  Also you have an undefined local
> variable in click count named now, which is used prior to having a
> value.
>  
> Richard
>         ----- Original Message ----- 
>         From: Storm Dragon 
>         To: programmingblind@xxxxxxxxxxxxx 
>         Sent: Sunday, December 13, 2009 6:15 AM
>         Subject: Strange error in Python code
>         
>         
>         
>         Hi,
>         I may have gone about this the wrong way. I wanted a simple
>         way to tell if a button has been pressed twice.  My class
>         gives a strange error though.  I wrote a quick bit of code to
>         test it.  It just calls the method twice in succession so it
>         should get 1 then 2 for output.  Instead, it says:
>         <bound method countPresses.clickCount of
>         <__main__.countPresses instance at 0x7f0a97e78c68>
>         Here's the code:
>         import time
>         class countPresses:
>             def __init__(self, clicks = 1, now = time.time()):
>                 pass
>         
>             def clickCount(self):
>                 if now + 0.75 >= time.time():
>                     self.clicks += 1
>                 now = time.time()
>                 if self.clicks > 2: self.clicks = 1
>                 return self.clicks
>         
>         test = countPresses()
>         print str(test.clickCount)
>         print str(test.clickCount)
>         
>         Thanks
>         Storm
>         
>         
>         -- 
>         Thoughts of a Dragon:
>         http://www.stormdragon.us/
>         What color dragon are you?
>         
> http://quizfarm.com/quizzes/new/alustriel07/what-color-dragon-would-you-be/
>         
>         
>         
>         

Other related posts: