We're aware that some users are experiencing technical issues which the team are working to resolve. See the Community Noticeboard for more info. Thank you for your patience.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

python code between 2 numbers

Options
sandy700
sandy700 Posts: 180 Forumite
Third Anniversary 100 Posts Photogenic
edited 25 November 2022 at 4:48PM in Techie Stuff
sandy700 said:
I am trying to write python code to get a relay to switch on when a reading is between two numbers.
here is the code

        # Get wetness
        wetness=wet.read()

        print("wetness",wetness)                       
       
        if wetness<400 :
            print("wet")
            relaypin.value(1)
            time.sleep(checkin)
        else:
            print("Dry")
            relaypin.value(0) 
            time.sleep(1)                     
            relaypin.value(1)

if I replace  if wetness<400  with 
                   
if wetness>=400 and wetness<=500

it then prints dry whereas I still want it to be wet.

What am I doing wrong?

Comments

  • shiraz99
    shiraz99 Posts: 1,836 Forumite
    1,000 Posts Third Anniversary Name Dropper
    Replace the and with or.
  • shiraz99
    shiraz99 Posts: 1,836 Forumite
    1,000 Posts Third Anniversary Name Dropper
    Why would you delete the post? It would be helpful for others. Quite rude tbh!
  • sandy700
    sandy700 Posts: 180 Forumite
    Third Anniversary 100 Posts Photogenic
    shiraz99 said:
    Replace the and with or.

    thanks I was rewriting my post and I copy and pasted a blank piece of code and deleted everything. 
  • sandy700 said:
    I am trying to write python code to get a relay to switch on when a reading is between two numbers.
    here is the code

            # Get wetness
            wetness=wet.read()

            print("wetness",wetness)                       
           
            if wetness<400  
                print("wet")
                relaypin.value(1)
                time.sleep(checkin)
            else:
                print("Dry")
                relaypin.value(0) 
                time.sleep(1)                     
                relaypin.value(1)

    if I replace  if wetness<400  with 
                       
    if wetness>=400 and wetness<=500

    it then prints dry whereas I still want it to be wet.

    What am I doing wrong?
    Answer deleted

    4.8kWp 12x400W Longhi 9.6 kWh battery Giv-hy 5.0 Inverter, WSW facing Essex . Aint no sunshine ☀️ Octopus gas fixed dec 24 @ 5.74 tracker again+ Octopus Intelligent Flux leccy
  • Billxx
    Billxx Posts: 294 Forumite
    Sixth Anniversary 100 Posts Name Dropper Photogenic
    Very odd, your syntax looks OK (apart from the missing colon).  This works:

    a = 450
    if a >= 300 and a <= 500:
      print("a is between")
    else:
      print("a is not between")

    Kind Regards,

    Bill
  • sandy700
    sandy700 Posts: 180 Forumite
    Third Anniversary 100 Posts Photogenic
    shiraz99 said:
    Why would you delete the post? It would be helpful for others. Quite rude tbh!
    Have you never clicked back then post and deleted everything you posted?
  • mksysb
    mksysb Posts: 406 Forumite
    Eighth Anniversary 100 Posts Photogenic Name Dropper
    a python way of doing it is 

    if 400 <= wetness <= 500:


  • shiraz99
    shiraz99 Posts: 1,836 Forumite
    1,000 Posts Third Anniversary Name Dropper
    edited 25 November 2022 at 9:41PM
    sandy700 said:
    shiraz99 said:
    Why would you delete the post? It would be helpful for others. Quite rude tbh!
    Have you never clicked back then post and deleted everything you posted?
    No, edited it maybe but not deleted everything along with the title.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 350.8K Banking & Borrowing
  • 253.1K Reduce Debt & Boost Income
  • 453.5K Spending & Discounts
  • 243.8K Work, Benefits & Business
  • 598.7K Mortgages, Homes & Bills
  • 176.8K Life & Family
  • 257.1K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.1K Discuss & Feedback
  • 37.6K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.