如何在numpy中最多3位精度的数组中输入实数?



实际上,我已经写了一个基本的代码,其中包括输入命令:f23=float(input("Enter The Element of Array:"))

但是它对数字没有任何限制,我想把它限制在3位。例如:2.253

有人知道该怎么做吗?

如果您想自动更正用户,那么您应该使用内置的round函数或format函数。

或者,如果你想在它这样做时引发异常,我建议将舍入后的版本与原始版本等同起来。

基本实现应该是这样的:

arr = []
no_digits = 3
for x in range(26):
arr[x] = round(float(input(f"the {x}th element is: ")),no_digits)

或带列表推导式:

arr = [round(float(input(f"the {x}th element is: ")),no_digits) for x in range(26)]

最新更新