我正在尝试在try...except
下面的代码中实现异常处理。当输入“s4”之类的内容时,我希望出现“Non numeric value...”的输出。
知道我哪里出错了吗?
import string
import math
def getSqrt(n):
return math.sqrt(float(n))
s = input("Enter a numerical value: ")
try:
for i in s:
if (i.isdigit() or i == "."):
sType = "nonstr"
if (sType =="nonstr"):
print(getSqrt(s))
else:
s = "Non numberical value..."
except ValueError as ex:
print(ex)
else:
print(s)