python - 使用 break 语句退出循环
问题描述
我想检测程序何时看到值 26,我有一个看起来像这样的函数
val = 26
for n in range (0,101):
if n in val:
print(n,"is the value i am looking for")
break
else:
print(n)
我不断收到此错误:
TypeError: argument of type 'int' is not iterable
这个功能有什么问题
解决方案
n in val
检查值n
是否在 iterable 中val
。这里正确的检查是n == val