if not teststr.isdigit()是真的,因此它需要计算int(teststr)来完成and的需求—因此是异常。你知道吗
不要预先检查数据,而是使用EAFP-并使用以下内容。。。你知道吗
try:
val = int(teststr)
if val != 1:
raise ValueError("wasn't equal to 1")
except (ValueError, TypeError) as e:
pass # wasn't the right format, or not equal to one - so handle
# carry on here...
if not teststr.isdigit()
是真的,因此它需要计算int(teststr)
来完成and
的需求—因此是异常。你知道吗不要预先检查数据,而是使用EAFP-并使用以下内容。。。你知道吗
not teststr.isdigit()
是真的,所以第一个测试不会失败。你知道吗评估为
好吧,但是
teststr
不是数字,所以isdigit()
是假的,所以(not isdigit())
是真的。对于True and B
,您必须计算B。这就是为什么它尝试将类型转换为int相关问题 更多 >
编程相关推荐