Check if user input is between two floats – Python

Issue

I am currently working on a small project that will take a user input such as "50", and convert it to a float while also placing the decimal to the left, such as "0.50" – This part I have working the way I want, but the issue I am having now that I cant seem to solve is checking if that value is between two other float values. Here is what I have so far.

value = float(input("Enter number: "))
value /= 100

if 0.61 <= value <= 69:
    value = value - 0.049 # don't worry about this part
    

elif 0.70 <= value <= 79:
    value = value - 0.10 # don't worry about this part
    


"""
if value >= 0.61:        
    value = value - 0.049

if value >= 0.70:        
    value = value - 1.5
"""

When I enter anything above 69, such as 70 or 71 and so on. The program does not seem to realize that I am trying to adjust the value differently compared to as if the input was 65, the program understands what to do just fine. At the bottom is something else I have tried but not getting any luck.

Am I using elif wrong? Why am I unable to get my second if statement to read properly? Or is there a function or something else out there that will let me check if the value is between two float ranges?

I appreciate the efforts.

Solution

welcome to SO.

EDIT:
Solution that should fit with your comment.

value = float(input("Input a number"))

if value >= 0.61 and value <= 0.69:
   #whatever should happen here
   

elif value >= 0.70 and value <= 0.79:
   #whatever you want to happen here

Do you really need to divide the value by 100, if so then you will never get into the second loop exclusively because the first if statement will be executed when your value is between 0.69 and 69 which is any value it can be if you divide it by 100, therefore it will never go into the second if statement.

If you do want to keep the /100 but execute BOTH statements then you can do it simply by changing the elif into and if so it also gets executed if the statement is true. This will execute BOTH if statements though.

value = float(input("Input a number"))
value /= 100

if value >= 0.61 and value <= 69:
   #whatever should happen here
   

if value >= 0.70 and value <= 79:
   #whatever you want to happen here

This way if the value entered is 70 the outcome will be that both if statements will be executed.

If you can omit the /100 then this code here works and only executes ONE if statement.

value = float(input("Input a number"))

if value >= 61 and value <= 69:
   #whatever should happen here
   

elif value >= 70 and value <= 79:
   #whatever you want to happen here

Answered By – Maximilian Sören Pollak

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.