Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使函数在python中执行n次_Python_Python 2.7 - Fatal编程技术网

如何使函数在python中执行n次

如何使函数在python中执行n次,python,python-2.7,Python,Python 2.7,在上面的代码中,我需要取“result”的值作为“a”的输入并执行10次。您应该首先从函数返回变量result: def sum_of_squares_of_digits(a): a = input("Enter the rpm ") s = sum(int(c) ** 2 for c in str(a)) print s m = s * 323 print m r = a%10 e = a/10 x = e%10 u =

在上面的代码中,我需要取“result”的值作为“a”的输入并执行10次。

您应该
首先从函数返回变量
result

def sum_of_squares_of_digits(a):
    a = input("Enter the rpm ")
    s = sum(int(c) ** 2 for c in str(a))
    print s
    m = s * 323
    print m
    r = a%10
    e = a/10
    x = e%10
    u = int(str(r) + str(e))
    f = u%10
    h = u/10
    l = h%10
    t = int(str(l) + str(f))
    result = m+t
    print u
    print t
    print result

sum_of_squares_of_digits(2)
然后您可以调用该函数并使用a执行10次:

def sum_of_squares_of_digits(a):
    ...
    return result

在上面的代码中,我需要取“result”的值作为“a”的输入并执行10次。你听说过循环吗?请把你的问题放在问题中,而不是评论中。告诉我们你想做什么,为什么不成功。
res = sum_of_squares_of_digits(2) # first call with 2 like in your program
for i in range(10):
    res = sum_of_squares_of_digits(res) # next 10 calls with the returned value