We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 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!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide
python code between 2 numbers
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
-
Answer deletedsandy700 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 leccy
CEC Email energyclub@moneysavingexpert.com0 -
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
- 353.5K Banking & Borrowing
- 254.1K Reduce Debt & Boost Income
- 455K Spending & Discounts
- 246.5K Work, Benefits & Business
- 602.8K Mortgages, Homes & Bills
- 178K Life & Family
- 260.5K Travel & Transport
- 1.5M Hobbies & Leisure
- 16K Discuss & Feedback
- 37.7K Read-Only Boards

