这个程序是求数组中3的倍数之和。帮我找出错误,使它工作。
values = [1,3,"5",7,8,9]
multiples_of_three = []
total_of_threes = 0
while not (i > values.lenght):
if (values[i] modulo 3) ==0:
multiples_of_three[i] == values[i]
while True:
if not multiples_of_three.empty():
total_of_threes = total_of_threes + multiples_of_three.first_item
multiples_of_three.remove_first_item
else if multiples_of_three.empty():
break
print (total_of_threes)
我不知道你到底想要什么。
现在,我做了一个预测,并写下了答案。
如果不是你想要的结果,在评论中写下细节。
试试这段代码(python风格):
values = [1, 3, "5", 7, 8, 9]
result = 0
for value in values:
if int(value) % 3 == 0:
result += value
print(result)
如果您想使用列表,解决方案1:
values = [1, 3, "5", 7, 8, 9]
multiples_of_three = []
result = 0
for value in values:
if int(value) % 3 == 0:
multiples_of_three.append(value)
for value in multiples_of_three:
result += value
print(result)
如果你想使用列表,解决方案2:
values = [1, 3, "5", 7, 8, 9]
multiples_of_three = []
result = 0
for value in values:
if int(value) % 3 == 0:
multiples_of_three.append(value)
print(sum(multiples_of_three))