python - 总是在我的代码中返回循环错误,想知道我需要做什么来修复它?
问题描述
假设 List[int] 已经被导入,我想知道这个问题的最后四行代码有什么问题。每当我尝试运行它时,我都会收到一条错误消息,指出“[第 11 行] 此循环只会运行一次迭代”。为缺乏术语或知识而道歉(我是 python 新手)
def divisible_by_7(lst: List[int]) -> bool:
"""Return True if and only if lst contains an element divisible
by 7. Otherwise, return False.
>>> divisible_by_7([4,8,21,6])
True
>>> divisible_by_7([1,2,8,9])
False
"""
for num in lst:
if num % 7 == 0:
return True
return False
解决方案
只需像这样移动“return False”:
for num in lst:
if num % 7 == 0:
return True
return False
推荐阅读
- python - 如何用不同的值替换 Keras 张量中的 nans(我有一个 tensorflow 实现)
- c# - 调用 Google Directory API 修补用户密码时出现间歇性 412 错误
- python - 如何优化代码以在 python pandas 中透视表
- flutter - 将键盘功能添加到颤动中的按钮(带按钮的自定义键盘)
- python - 列出子网python boto3中的可用区
- jquery-ui - 如何使用 jQuery-UI 隐藏按钮集小部件中的单选按钮点?
- python - 是否可以使用 python 和 sci-kit learn 执行概率 PCA?
- javascript - 如何强制 Tagify 只接受由小写和下划线组成的标签?
- java - 如何知道@JsonIgnoreProperties(ignoreUnknown=true) 忽略了哪些属性
- java - 尝试从“主菜单”类调用方法但它被卡住了,但是当从方法类调用时它工作得很好