英文:
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("Enter an CVSS Score: "))
if score <= 0:
print("Risk Score = None")
elif score in range(0.1, 3.9):
print("Risk Score = Low")
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.1
和 3.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 float
s (0.1
and 3.9
). You can fix your code by changing it to:
score = float(input("Enter an CVSS Score: ")) # cast input to float
if score <= 0:
print("Risk Score = None")
elif 0.1 <= score < 3.9: # check input is within bounds
print("Risk Score = Low")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论