-2

im trying to return the divisors of 2 numbers that i appended to an empty list. why is nothing being printed? im expecting for 1,2,3 to be returned to mw but i get returned "[]" and "none"

def fun (t,g) :
    list1 = []
    i = 1
    r = 1
    while i < t :
        if i % t == 0 :
            list1.append(i)
        i = i + 1
    while r < g :
        if r % g == 0 :
            list1.append(r)
        r = r + 1
    print list1

x = 4
y = 6

t = fun(x,y)
print t
4

1 回答 1

1

i % t永远不会0,因为您正在退出 while 循环 when i == t。也许你的意思是t % i

对于r和 也是如此g

你的函数没有 areturn所以它会隐式返回None

您应该添加return list1到它的末尾。

def fun (t,g) :
    list1 = []
    i = 1
    r = 1
    while i < t :
        if t % i == 0 :
            list1.append(i)
        i = i + 1
    while r < g :
        if g % r == 0 :
            list1.append(r)
        r = r + 1
    print list1
    return list1

x = 4
y = 6

t = fun(x,y)
print t

印刷

[1, 2, 1, 2, 3]
[1, 2, 1, 2, 3]

所以你仍然需要计算出重复项

于 2013-10-23T05:49:08.740 回答