Python for循环和函数

时间:2018-09-21 14:54:37

标签: python python-3.x

是python的新手,我一直在尝试解决此问题,但它似乎无法按预期工作。非常感谢您的帮助:

给出两个数字X和Y,编写一个函数:

  • 如果X大于Y,则返回X和Y之间的偶数
  • else返回x和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))

6 个答案:

答案 0 :(得分:1)

就完成了。基本上,如果是x > y,则需要切换第一个range。您可以正常地附加项目(使用()而不是[]),然后返回完整列表,知道吗?

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

工作示例:https://py3.codeskulptor.org/#user302_nwBq00w56n_1.py

答案 1 :(得分:1)

range(start,stop[,step]) with a step of 2而不是一直测试奇数/偶数为例,而是以{(校正的,已知的)奇数/偶数开头:

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))
  • 如果x,您还需要切换yx > y
  • 您不需要迭代范围并将其元素迭代地添加到list中-只需将范围序列填充到the list(sequence) constructor中并返回

输出:

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

答案 2 :(得分:1)

尝试此代码即可根据您的需要运行。

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

输出为:

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

让我知道这是否不符合您的目的。

答案 3 :(得分:0)

这很容易做到,有几种方法可以完成您想要的事情,所以我向您展示了两种方法,一种可以理解的方法,第二种简单的方法,让我们开始吧:-

  

第一个例子

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]
  

第二个例子

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]

注意:,但是请确保在我的两个答案中,x号都包含在内(在搜索功能中),而y号是排斥性的,所以如果您想要使它们都包含在内,因此要使循环...(x, y+1)...,如果要使它们都具有排他性,只需将循环更改为...(x+1, y)...

答案 4 :(得分:0)

在这里我使用list comprehensions.list comprehension是python中一种简单易懂的技术。在此我同时包含x和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

答案 5 :(得分:0)

知道2 % 2 == 0之后就可以使用if not 2 % 2进行偶校验,因为not 0的计算结果为true,这是理解和扩展形式

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]

完整循环:

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