检查用户输入是否在两个浮子之间 - python

发布于 2025-01-28 19:32:14 字数 705 浏览 1 评论 0原文

我目前正在研究一个小型项目,该项目将吸引用户输入,例如“ 50”,并将其转换为浮点,同时还将小数放在左侧,例如“ 0.50” - 我已经按照我想要的方式来工作,但是我现在似乎无法解决的问题是检查该值是否在其他两个浮点值之间。这是我到目前为止所拥有的。

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
"""

当我输入以上69以上的内容时,例如70或71,依此类推。该程序似乎并没有意识到我试图以不同的方式调整该值,就像输入为65一样,该程序可以理解该怎么做。在底部是我尝试过的其他事情,但没有任何运气。

我在使用埃利夫吗?为什么我无法获得第二个IF语句以正确阅读?还是那里有一个函数或其他可以让我检查该值是否在两个浮点范围之间?

感谢他们的努力。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2025-02-04 19:32:15

欢迎来到。

编辑:
应符合您的评论的解决方案。

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

您是否真的需要将值除以100,如果是这样,您将永远不会专门进入第二个循环,因为当您的值在0.69和69和69之间,这是第一个IF语句如果您将其除以100,因此它永远不会进入第二个If语句。

如果您确实要保留/100但执行两个语句,那么您可以通过将elif和如果更改为来完成此操作。如果语句,这将执行这两个。

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

这样,如果输入的值为70,结果将是如果将语句执行。

如果您可以省略 /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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文