[SOLVED] How to generate even numbers with a length limit?

Issue

I want to generate a list of even but want to specific how many numbers to be added to the result too.

start, n = 2, 5

def fEven(start, n):
    #do something
fEven(start, n)

This should generate 2,4,6,8,10 as 5 was specified for the total number to be generated

Solution

This is the correct answer as all the above answers are just a workaround to print n*2 in a loop.

What if it starts with an odd number? this handles both cases.

def fEven(start, n):
    res = []
    if(start%2==0):
      for i in range(n):
          res.append(start)
          start += 2
      return res
    else:
      start +=1
      for i in range(n):
          res.append(start)
          start += 2
      return res

Answered By – Parzival

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published.