我想检查字符串中是否有任何字母数字字符。我为此编写了以下代码,并且工作正常:
s = input()
temp = any(i.isalnum() for i in s)
print(temp)
Run Code Online (Sandbox Code Playgroud)
我的问题是下面的代码,它与上面的代码有何不同:
for i in s:
if any(i.isalnum()):
print(True)
Run Code Online (Sandbox Code Playgroud)
for循环迭代仍在第一个代码中进行,为什么它不引发错误?第二个代码抛出:
追溯(最近一次调用):TypeError中的文件“”,第18行:“ bool”对象不可迭代