Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I check if the input() is in a given interval?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading