Reputation: 13
In python 3.x I want to run the while loop for all the elements of mass_list and the output should also be in a list. but my loop is taking only the last value.
input:
mass_list = [1969,100756]
expected output:
[966,50346]
my code is---
for a in mass_list:
mass_sum = 0
total_mass_sum = []
while a >= 6:
i = (int (a/3)-2)
mass_sum = i+mass_sum
a = i
total_mass_sum.append(mass_sum)
print(total_mass_sum)
where am I going wrong?
Upvotes: -1
Views: 1129
Reputation: 9494
The initialization of total_mass_sum
must be outside the for
loop in order to prevent reseting of the output payload for each iteration and the append to total_mass_sum
must be inside the for
loop:
mass_list = [1969,100756]
total_mass_sum = []
for a in mass_list:
mass_sum = 0
while a >= 6:
i = (int (a/3)-2)
mass_sum = i+mass_sum
a = i
total_mass_sum.append(mass_sum)
print(total_mass_sum) # output: [966, 50346]
Upvotes: 1