-1

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?

Gabio
  • 9,126
  • 3
  • 12
  • 32
Anu
  • 13
  • 4
  • 1
    Please _indent_ your code. It is syntactically incorrect. It is also unclear what you are trying to do with each element. – DYZ May 01 '20 at 08:04
  • `total_mass_sum(mass_sum)= [966,50346]` I don't believe that's valid Python. – AMC May 01 '20 at 08:19
  • Okey..thanks for pointing out..I am a newbie...will keep that point in mind – Anu May 01 '20 at 11:29

1 Answers1

1

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]                       
Gabio
  • 9,126
  • 3
  • 12
  • 32