Laurentiu Ionut
Laurentiu Ionut

Reputation: 35

python check two values to be between two values

I have an issue, I want to verify if two values are between two values like:

val1 = 23.04
val2 = 29.04

tobe1 = 24.04
tobe2 = 27.04
if tobe1, tobe2 in range(val1, val2):
   print("something")

Upvotes: 0

Views: 2468

Answers (2)

Rajarishi Devarajan
Rajarishi Devarajan

Reputation: 581

This code should get you the required result

val1 = 23.04
val2 = 29.04

tobe1 = 24.04
tobe2 = 27.04

your_list = [tobe1, tobe2]
if all(val1 < x < val2 for x in (tobe1, tobe2)):
    print("something")

If you want ALL values in (tobe1, tobe2) to be within the val1 and val2, then use all

If you want ANY value in (tobe1, tobe2) to be within the val1 and val2, then use any.

Upvotes: 0

Tin Nguyen
Tin Nguyen

Reputation: 5330

>>> 3 < 5
True
>>> 3 < 4 < 6
True
>>> 3 < 7 < 6
False

range() method does something different than you expect. Use simple < comparators. You can replace my example values with variables.

Upvotes: 3

Related Questions