Evans
Evans

Reputation: 31

Python for loop and function

Am new to python and I have been trying to solve this problem but it does not seem to work as intended. your help is highly appreciated:

Given two numbers X and Y, write a function that:

.

def number(x,y):
    if x > y:
        for i in range(x,y):
            if i%2 == 0:
                list = []
        return list.append[i]
    else:
        for i in range(x,y):
            if i%2 == 1:
                list = []
        return list.append[i]

print(number(10,2))

Upvotes: 2

Views: 1981

Answers (6)

satishreddy
satishreddy

Reputation: 19

Here in this i use the list comprehensions.list comprehension is a easy and readable technique in python.In this i include both x and y

def fun(x,y):
    if x>y:
        l=[i for i in range(y,x-1) if i%2==0]
        return l.reverse()
    else:
        l=[i for i in range(x,y+1) if i%2!=0]
    return l

Upvotes: 0

vash_the_stampede
vash_the_stampede

Reputation: 4606

Knowing that 2 % 2 == 0 we then can just use if not 2 % 2 for evens since not 0 will evaluate to true, here it is with comprehension and in extended form

def something(x, y):
    if x > y:
        l = [i for i in range(y, x) if not i % 2]
    else:
        l = [i for i in range(x, y) if i % 2]
    return l

print(something(10, 2))
print(something(2, 10))
~/python/stack$ python3.7 sum.py 
[2, 4, 6, 8]
[3, 5, 7, 9]

Full loop:

def something(x, y):
    l = []
    if x > y:
        for i in range(y, x):
            if not i % 2:
                l.append(i)
    else:
        for i in range(x, y):
            if i %2:
                l.append(i)
    return l

Upvotes: 0

Azhy
Azhy

Reputation: 706

It's so easy to do, and there are several ways to do what do you want, so i show you two ways to do that, first an understandable way and second an easy way ok let's start:-

First example

def number(x,y):

    list = []  #firstly create a list

    if x > y:  #if x was greater than y
        for num in range(y, x): # a loop for searching between them
            if(num % 2 == 0):   # if the number was even add it to list
                list.append(num)
    elif y > x: #if y was greater than x
        for num in range(x, y): # a loop for searching between them
            if(num % 2 != 0):   # if the number was not even add it to list
                list.append(num)

    return list

print(number(10, 20))
print(number(20, 10))

#[11, 13, 15, 17, 19]
#[10, 12, 14, 16, 18]

Second example

number = lambda x, y : [n for n in range(y, x) if n%2 == 0] if x > y else [n for n in range(x, y) if n%2 != 0]

print(number(10, 20))
print(number(20, 10))

#[11, 13, 15, 17, 19]
#[10, 12, 14, 16, 18]

Note : But be sure that in both of my answers the x number is inclusive(exists in searching function) and the y number is exclusive, so if you wanted to make both of them inclusive so make loops ...(x, y+1)... and if you wanted to make both of them exclusive just change loops to ...(x+1, y)....

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51623

Instead of testing for oddness/evenness all the time, use range(start,stop[,step]) with a step of 2 starting with a (corrected, known) odd/even number:

def number(x,y):
    if x > y:
        if y%2 == 1: # y is smaller && odd
            y += 1 # make even
        return list(range(y,x,2)) # x is > y - start from y to x
    else: # this is strictly not needed - but more verbose intention-wise
        if x%2 == 0: # is even
            x += 1 # make odd
        return list(range(x,y,2))


print(number(10,32))
print(number(10,2))
  • You need to also switch x and y if x > y
  • you do not need to iterate a range and add its element to a list iteratively - simply stuff the range-sequence into the list(sequence) constructor and return it

Output:

[11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]
[2, 4, 6, 8]

Upvotes: 1

Hackerman
Hackerman

Reputation: 12305

And it is done. Basically if x > y, you need to switch the first range. You append the items normally(using () instead of []), and then return the full list, got it?

    def number(x,y):
        list = []
        if x > y:
            for i in range(y,x):
                if i%2 == 0:
                    list.append(i)
        else:
            for i in range(x,y):
                if i%2 == 1:                
                    list.append(i)

        return list

    print(number(10,2))

Working sample: https://py3.codeskulptor.org/#user302_nwBq00w56n_1.py

Upvotes: 1

Akhilesh Pandey
Akhilesh Pandey

Reputation: 896

Try this code it's working as per your need.

def number(x,y):
    num= []
    if x > y:
        for i in range(y,x):
            if i%2 == 0:
                num.append(i)
    else:
        for i in range(x,y):
            if i%2 == 1:
                num.append(i)
    return num

print(number(2,10))
print(number(10,2))

The outputs are:

[3, 5, 7, 9]
[2, 4, 6, 8]

Let me know if this doesn't serve your purpose.

Upvotes: 1

Related Questions