2

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:

  • returns even numbers between X and Y, if X is greater than Y
  • else returns odd numbers between x and y

.

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))
3
  • Your code is almost there...let me give it a look Commented Sep 21, 2018 at 14:56
  • return list.append[i]. Lots to unpack here. To call a function you need to use parenthesis. list.append returns None, so this will too. Finally, this will return the first time you add something to your list. You instead want to return after you've finished adding items to the list. You also shouldn't use the name list, as it already has a meaning. Commented Sep 21, 2018 at 14:57
  • 1
    Please make sure the indentation is correct. You may be creating empty lists in the wrong places (in addition to some syntactic errors) Commented Sep 21, 2018 at 14:57

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

I write a similar code. I didn't post it because you did first. So I upvote your answer. He should accept your answer.
Thank you @ThexPhi
@Hackerman thanks alot. it worked. problem was with indentation and declaring my list at the wrong place
No problem @Evans ...I fixed the url, so you can see the code working live.
1

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]

Comments

0

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)....

Comments

0

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

Comments

0

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

1 Comment

Please explain also in words why you wrote the function like that. This will be helpful to others for understanding why this is a better way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.