简体   繁体   English

检查用户输入是否在两个浮点数之间 - Python

[英]Check if user input is between two floats - Python

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.我目前正在做一个小项目,该项目将接受用户输入,例如“50”,并将其转换为浮点数,同时还将小数点放在左侧,例如“0.50” - 这部分我按照我想要的方式工作,但是我现在似乎无法解决的问题是检查该值是否介于其他两个浮点值之间。 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.当我输入任何高于 69 的值时,例如 70 或 71 等等。 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.程序似乎没有意识到我正在尝试以不同的方式调整值,就像输入是 65 一样,程序知道该做什么就好了。 At the bottom is something else I have tried but not getting any luck.在底部是我尝试过但没有得到任何运气的其他东西。

Am I using elif wrong?我使用 elif 错了吗? Why am I unable to get my second if statement to read properly?为什么我的第二个 if 语句无法正确阅读? 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.我很欣赏这些努力。

welcome to SO.欢迎来到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.您是否真的需要将该值除以 100,如果是这样,那么您将永远不会专门进入第二个循环,因为当您的值介于0.69 and 69之间时,将执行第一个 if 语句,如果您将它除以它可以是任何值100,因此它永远不会进入第二个 if 语句。

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.如果您确实想保留 /100 但同时执行 BOTH 语句,那么您只需将elif更改为, if这样,如果语句为真,它也会被执行。 This will execute BOTH if statements though.这将执行两个 if 语句。

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.这样,如果输入的值为70 ,则结果将是两个 if 语句都将被执行。

If you can omit the /100 then this code here works and only executes ONE if statement.如果您可以省略 /100,那么这里的代码可以工作并且只执行一个 if 语句。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM