x = int(input("Start: "))
y = int(input("End: "))
z = range (x,y)
print (z)
Interval_1= range(1, 5)
Interval_2 = range (6, 8)
Interval_3 = range (9, 10)
if z in Interval_1:
print ("Number is in Interval 1")
else: ("Number is not in Interval 1")
I want the user to enter 2 numbers and to a given interval it should check whether it is in the interval or not but it seems like the code does not even run the if query. Is it because of the "in" command?
>Solution :
Please try the below code. It checks whether the start and end are in interval or not and outputs accordingly.
x = int(input("Start: "))
y = int(input("End: "))
z = range (x,y)
print (z)
Interval_1= range(1, 5)
Interval_2 = range (6, 8)
Interval_3 = range (9, 10)
if z[0] in Interval_1 and z[-1] in Interval_1:
print ("Number is in Interval 1")
elif z[0] in Interval_2 and z[-1] in Interval_2:
print ("Number is in Interval 2")
elif z[0] in Interval_3 and z[-1] in Interval_3:
print ("Number is in Interval 3")
else:
print("Number is not in any of the intervals")