我正在编写一个脚本来将一个数字返回给一定数量的有效数字.我需要将浮动转换为列表,以便我可以轻松更改数字.这是我的代码:
def sf(n,x):
try:
float(n)
isnumber = True
except ValueError:
isnumber = False
if isnumber == True:
n = float(n)
n = list(n)
print(n)
else:
print("The number you typed isn't a proper number.")
sf(4290,2)
Run Code Online (Sandbox Code Playgroud)
这会返回错误:
Traceback (most recent call last):
File "/Users/jacobgarby/PycharmProjects/untitled/py package/1.py", line 29, in <module>
sf(4290,2)
File "/Users/jacobgarby/PycharmProjects/untitled/py package/1.py", line 25, in sf
n = list(n)
TypeError: 'float' object is not iterable
Run Code Online (Sandbox Code Playgroud)
这个错误意味着什么,我怎么能阻止它发生?