given_list = [7, 5, 4, 4, 3, 1, -2, -3, -5, -7]
total = 0
i = 0
while i < len(given_list) and given_list[i] <= 0:
total += given_list[i]
i += 1
print(total)
我正在使用 Jupyter 笔记本,并按照 CSdojo 在 youtube 上的 Python 教程进行操作。我想知道为什么当我运行我的代码时,它后面的单元不产生输出(总计)?
问题是given_list[i] <= 0
。
先i = 0
然后given_list[i] = 7
7 不等于或小于 0,则true and false = false
。这就是为什么它不显示输出。在这种情况下,您使用 Jupyter 或 Python 没有任何问题。你的问题是你的逻辑。请在算法而不是语法上努力。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句