使用小数范围

huangapple go评论51阅读模式
英文:

Using range with decimals

问题

我不断收到这段代码的错误代码,不知道如何解决。

score = int(input("输入 CVSS 分数: "))
if score <= 0:
    print("风险评分 = 无")
elif score in range(1, 4):
    print("风险评分 = 低")

我只需要能够输入 1-10 的数字,但也允许输入 1.2 或 3.4 之类的数。

英文:

I keep getting an error code for this code and don't know how to get around it.

score = int(input(&quot;Enter an CVSS Score: &quot;))
if score &lt;= 0:
    print(&quot;Risk Score = None&quot;)
elif score in range(0.1, 3.9):
    print(&quot;Risk Score = Low&quot;)

I just need to able to only put in numbers 1-10 but also allow things like 1.2 or 3.4.

答案1

得分: 3

以下是翻译好的内容:

来自文档
> range 构造函数的参数必须是整数

您正在尝试使用 range 与浮点数 (0.13.9)。您可以通过将代码更改为以下方式来修复:

score = float(input("输入 CVSS 分数: "))  # 将输入强制转换为浮点数
if score <= 0:
    print("风险分数 = 无")
elif 0.1 <= score < 3.9:  # 检查输入是否在范围内
    print("风险分数 = 低")
英文:

From the docs:
> The arguments to the range constructor must be integers

You're attempting to use range with floats (0.1 and 3.9). You can fix your code by changing it to:

score = float(input(&quot;Enter an CVSS Score: &quot;))  # cast input to float
if score &lt;= 0:
    print(&quot;Risk Score = None&quot;)
elif 0.1 &lt;= score &lt; 3.9:  # check input is within bounds
    print(&quot;Risk Score = Low&quot;)

huangapple
  • 本文由 发表于 2023年4月17日 03:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定