Results 1 to 7 of 7

Thread: Python if statement issue with numbers

  1. #1
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    5,169
    Distro
    Kubuntu

    Python if statement issue with numbers

    so i have a loop i can counting when it reaches 5 or 10 i want it to do something different
    my issue is with the line "if cnt == 0 or cnt == 5 or cnt == 10:"
    it only return true on when cnt is 0 i have tried 5 and 5.0 nigher returned true when the count reached 5/5.0
    Code:
    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    
    pin = 19
    pin2 = 21
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(pin2, GPIO.OUT)
    delay = 0.1
    
    try:
            while True:
                    cnt = 0
                    while GPIO.input(pin) == 1:
                            if cnt == 0 or cnt == 5 or cnt == 10:
                                    print cnt
                                    GPIO.output(pin2, 1)
                                    time.sleep(delay)
                                    GPIO.output(pin2, 0)
                                    cnt=cnt+delay
                                    time.sleep(cnt)
                            else:
                                    cnt=cnt+delay
                                    time.sleep(delay)
                    if cnt > 5 and cnt < 10:
                            # 5 to 10 seconds
                            print "5"
                    elif cnt > 9:
                            # 10 seconds or more
                            print "10"
                    elif cnt > 0:
                            # Less than 5 seconds
                            print "0"
                    time.sleep(delay)
    except KeyboardInterrupt:
            GPIO.cleanup()
    I have also tried this, still only works on 0
    if cnt in [0, 5, "5.0", 5.0, 10, "10.0", 10.0]:
    Last edited by pqwoerituytrueiwoq; August 4th, 2014 at 08:37 PM. Reason: see post #6
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Python if statement issue with numbers

    Your problem is comparing a plain int (0, 5, 10) with a float which is the result of computations and may not be exactly equal to 5.00000000000000000 or 10.0000000000000000. Try this:

    Code:
    cnt=0
    delay=.1
    for i in range(50):
      x=x+delay
      print '%3.20f' % x
    Last edited by ofnuts; August 4th, 2014 at 05:45 PM.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  3. #3
    Join Date
    Apr 2005
    Location
    My dreams
    Beans
    3,558
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Python if statement issue with numbers

    Did you put cnt = 0 in the while loop just for testing?
    Code:
            while True:
                    cnt = 0

  4. #4
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    5,169
    Distro
    Kubuntu

    Re: Python if statement issue with numbers

    Quote Originally Posted by kostkon View Post
    Did you put cnt = 0 in the while loop just for testing?
    Code:
            while True:
                    cnt = 0
    to reset the cnt varable before testing it again
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

  5. #5
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    5,169
    Distro
    Kubuntu

    Re: Python if statement issue with numbers

    Quote Originally Posted by ofnuts View Post
    Your problem is comparing a plain int (0, 5, 10) with a float which is the result of computations and may not be exactly equal to 5.00000000000000000 or 10.0000000000000000. Try this:

    Code:
    cnt=0
    delay=.1
    for i in range(50):
      x=x+delay
      print '%3.20f' % x
    still nothing
    Code:
    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    
    pin = 19
    pin2 = 21
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(pin2, GPIO.OUT)
    delay = 0.1
    five='%3.20f' % 5
    ten='%3.20f' % 10
    try:
            while True:
                    cnt = 0
                    while GPIO.input(pin) == 1:
                            if cnt in [0, five, 5, 5.0, "5.0", ten, 10, 10.0, "10.0"]:
                                    print "Hello"
                                    GPIO.output(pin2, 1)
                                    time.sleep(delay)
                                    GPIO.output(pin2, 0)
                                    cnt=cnt+delay
                            else:
                                    cnt=cnt+delay
                                    time.sleep(delay)
                    if cnt > 5 and cnt < 10:
                            # 5 to 10 seconds
                            print "5 - done"
                    elif cnt > 9:
                            # 10 seconds or more
                            print "10 - done"
                    elif cnt > 0:
                            # Less than 5 seconds
                            print "0 - done"
                    time.sleep(delay)
    except KeyboardInterrupt:
            GPIO.cleanup()
            print "\n"
    Last edited by pqwoerituytrueiwoq; August 4th, 2014 at 06:24 PM.
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

  6. #6
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    5,169
    Distro
    Kubuntu

    Re: Python if statement issue with numbers

    found a solution
    http://stackoverflow.com/questions/6...alue-in-python
    Code:
    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    
    def equal_float(a, b):
            return abs(a - b) <= 0.00000000001
    
    pin = 19
    pin2 = 21
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    GPIO.setup(pin2, GPIO.OUT)
    delay = 0.1
    
    try:
            while True:
                    cnt = 0
                    while GPIO.input(pin) == 1:
                            if cnt == 0 or equal_float(cnt, 5) or equal_float(cnt, 10):
                                    GPIO.output(pin2, 1)
                                    time.sleep(delay)
                                    GPIO.output(pin2, 0)
                                    cnt=cnt+delay
                            else:
                                    cnt=cnt+delay
                                    time.sleep(delay)
                    if cnt > 5 and cnt < 10:
                            # 5 to 10 seconds
                            print "5 - done"
                    elif cnt > 9:
                            # 10 seconds or more
                            print "10 - done"
                    elif cnt > 0:
                            # Less than 5 seconds
                            print "0 - done"
                    time.sleep(delay)
    except KeyboardInterrupt:
            GPIO.cleanup()
            print "\n"
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Python if statement issue with numbers

    Quote Originally Posted by pqwoerituytrueiwoq View Post
    Yes, this is the very same explanation I was mentioning... but in your case this isn't a very good solution. You should change your logic to count the number of times you use the delay, so you would compare integers to integers.
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •