We'd like to remind Forumites to please avoid political debate on the Forum... Read More »
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
Posts: 180 Forumite

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 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?
it then prints dry whereas I still want it to be wet.
What am I doing wrong?
0
Comments
-
Replace the and with or.0
-
Why would you delete the post? It would be helpful for others. Quite rude tbh!1
-
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 wetnesswetness=wet.read()print("wetness",wetness)if wetness<400print("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 withif wetness>=400 and wetness<=500
it then prints dry whereas I still want it to be wet.
What am I doing wrong?
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 leccy0 -
Very odd, your syntax looks OK (apart from the missing colon). This works:
a = 450if a >= 300 and a <= 500:print("a is between")else:print("a is not between")
Kind Regards,
Bill0 -
a python way of doing it is
if 400 <= wetness <= 500:
1 -
Confirm your email address to Create Threads and Reply

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