Home > Net >  How do i change the loop so it chooses specific elements in a list?
How do i change the loop so it chooses specific elements in a list?

Time:12-16

As the title says, how do i change the loop so it chooses specific elements in a list?

In this case i need to choose and only use the first 10 elements.


AGE_DATA =[22, 38, 26, 35, 35, 54, 2, 67, 14, 4, 68, 30, 41, 14, 95, 2]

def comAvAge(arg1: list):
   count = 0
   sum = 0
   for x in arg1:
      sum = sum   x
      count = count   1
   avg = sum / count
   return avg

titanicAge = comAvAge(AGE_DATA) 
print(titanicAge)

CodePudding user response:

Try:

for x in arg1[0:10]:
    sum = sum   x
    count = count   1
avg = sum / count

CodePudding user response:

your question:

AGE_DATA ='22 38 26 35 35 54 2 27 14 4 58 20 39 14 55 2'.split()
print(AGE_DATA[0:10])
  • Related