[英]Adding to a variable inside a loop
previous_num = 0
sum = 0
def num_update():
add_num = current_num - previous_num
if add_num > 0:
sum += add_num
previous_num = current_num
elif add_num < 0:
previous_num = current_num
current_num usually increases but sometimes decreases to a lower number than previous. current_num通常会增加,但有时会减少到比以前更低的数字。
It's only updating the first element in the list. 它只是更新列表中的第一个元素。
list = [a, b, c, d]
for i in list:
num_update()
Can you elaborate what you're trying to do here? 您能在这里详细说明您要做什么吗? We need context as to what you're trying to achieve.
我们需要有关您要达到的目标的环境。
Face-off, here are a few problems I've spotted though. 直面,这是我发现的一些问题。
previous_num = 0
sum = 0
def num_update():
add_num = current_num - previous_num
if add_num > 0:
sum += add_num
previous_num = current_num
elif add_num < 0:
previous_num = current_num
You were missing the indentation for the first line of your definition 您缺少定义第一行的缩进
In your for i... list iteration, 在您的...清单迭代中,
You should be using it in the following format: 您应该以以下格式使用它:
for i in range(len(list)):
//code
Let me know if this fixes your issue, if you want to iterate through a list, you need to use the length of the list. 让我知道这是否解决了您的问题,如果要遍历列表,则需要使用列表的长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.