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笔记本,并在youtube上按照CSdojo的python教程进行操作。我想知道为什么当我运行我的代码时,单元格后面没有产生输出(总计)?
答案 0 :(得分:0)
问题是given_list[i] <= 0
。
首先i = 0
然后given_list[i] = 7
7不等于或小于0,然后是true and false = false
。这就是为什么它没有显示输出。这种情况下你对Jupyter或Python没有任何问题。你的问题是你的逻辑。请尽量使用算法而不是语法。