Home > Blockchain >  How do i add the numbers of multiples in a list?
How do i add the numbers of multiples in a list?

Time:09-29

Im trying to find the number of multiples in a list while divided by a number provided from the user. For example the list [12, 5] and the user input is 3 so the output would be 5, because int(12/3) int(5/3) = 5.

def multiple(first_list, dividing_num):
     quotient = []
     for i in list(first_list):
      quotient = i // dividing_num
      i  = quotient
     return i
print(multiple([9, 18, 2, 20, 21], 4))
#the output should be 16

CodePudding user response:

few changes in your for loop

summ = 0
for i in first_list:
    quotient = i // dividing_num
    summ  = quotient
return summ

quotient = [] can be removed

CodePudding user response:

You can actually use a very short comprehension here:

l = [12, 5]
n = 3
sum(i//n for i in l)

output: 5

  • Related