简体   繁体   English

如何在循环中使用函数中的结果?

[英]How can I use the results in functions in a loop?

I want to define two functions, one inside the other, and loop it until it reaches the value I'm looking for.我想定义两个函数,一个在另一个内部,然后循环它直到它达到我正在寻找的值。 However, the values in the function are not accessible from outside the function, so the codes do not work properly.但是,function 中的值无法从 function 外部访问,因此代码无法正常工作。 Can I get some help with that?我能得到一些帮助吗? Is there a way to achieve this?有没有办法做到这一点?

def count_div(number):
    counter = 0
    for ite in range(1,number+1):
        if number % ite == 0:
            counter += 1
        else:
            continue
    else:
        #print(number, counter)
        return counter

def sum_of(order):
    num = 0
    for a in range(order+1):
        num = num + a  
    count_div(num)

#sum_of(7)

for o1 in range(100):
    if sum_of(o1) == 20:
        print(o1)
        break
    else:
        continue
else:
    print('can\'t reach') 

there is my work but in for loop sum_of func.有我的工作,但在 for loop sum_of func 中。 not give an usable result for comparison.. Thanks You.没有给出一个可用的比较结果。谢谢你。

You need to return the result of the function you call.您需要返回您调用的 function 的结果。

def sum_of(order):
    num = 0
    for a in range(order+1):
        num = num + a
    return count_div(num)

When there is no explicit return statement in a function python automatically returns None .当 function python 中没有明确的 return 语句时,会自动返回None Therefore the line if sum_of(o1) == 20: was comparing None to 20 every iteration.因此, if sum_of(o1) == 20:这一行在每次迭代时都将None20进行比较。

Just FYI for the function count_div you can use sum for a more pythonic approach:对于 function count_div仅供参考,您可以使用sum来获得更Python的方法:

sum(not number%(n+1) for n in range(number)

And sum_of this is just adding all positive integers from 1-n.sum_of这只是将 1-n 中的所有正整数相加。 This can also be done using this equation n*(n+1)//2 for sake of efficiency vs a looping method, thus making sum_of pretty much irrelevant.这也可以使用这个方程n*(n+1)//2来完成,以提高效率与循环方法,从而使sum_of几乎无关紧要。

def count_div(num):
    return sum(not num%(n+1) for n in range(num*(num+1)//2))

Then for your last loop:然后对于你的最后一个循环:

for o1 in range(100):
    if count_div(o1) == 20:
        print(o1)
        break

Your sum_of function is not returning anything which is why your if condition will never work.您的sum_of function 没有返回任何内容,这就是为什么您的 if 条件永远不会起作用的原因。 Add a return on the last line in sum_of function:sum_of function 的最后一行添加返回:

return count_div(num)

Also you have lots of unnecessary else: statements which you should remove, here is an example:此外,您还有很多不必要的else:您应该删除的语句,这是一个示例:

def count_div(number):
    counter = 0
    for ite in range(1,number+1):
        if number % ite == 0:
            counter += 1

    #print(number, counter)
    return counter

def sum_of(order):
    num = 0
    for a in range(order+1):
        num = num + a  
    return count_div(num)

#sum_of(7)

for o1 in range(100):
    if sum_of(o1) == 20:
        print(o1)
        break
else:
    print('can\'t reach') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM