0

Problem: Finding the Odd numbers within given range(inclusive). l = left range, r = right range. For example:

Input:

l=2
r=5

Expected Output:

3
5

As I have already done this program in one method shown in below,

def findOddNumber(l,r):
for num in range(l,r):
    if(num%2!=0):
        print(num)
l=2
r=5
res=[]
findOddNumber(l,r)

Instead of printing in user defined function like above, I'm expecting to return the values to calling function, I have tried like this:

def findOddNumber(l,r):
for num in range(l,r):
    if(num%2!=0):
        return num
l=2
r=5
res=findOddNumber(l,r)
print(res)

Output:

3

Expected output:

3
5
Goutham Vijay
  • 41
  • 2
  • 7
  • 1
    You can only `return` once. You could build and return a sequence (list, tuple, ...) or look into generators that can `yield` multiple values. – jonrsharpe Jan 25 '19 at 11:25

1 Answers1

0

You can use a list and instead of printing just append to the list. Then you return the list

def findOddNumber(l,r):
    res=[]
    for num in range(l,r+1):
        if(num%2!=0):
            res.append(num)
    return res

l=2
r=5
res=findOddNumber(l,r)
print(res)
nacho
  • 5,280
  • 2
  • 25
  • 34