def printFun(test):
if (test < 1):
return
else:
print( test, end = " ")
printFun(test-1) # statement 2
print( test, end = " ")
return
test = 3
printFun(test)
谁能在调用printFunc(0)之后解释该算法的工作原理
def printFun(test):
if (test < 1):
return
else:
print( test, end = " ")
printFun(test-1) # statement 2
print( test, end = " ")
return
test = 3
printFun(test)
谁能在调用printFunc(0)之后解释该算法的工作原理
我会帮您分解此代码。 第一,
I assume you know that this means that if
test
is smaller than 1, the function returns a value and terminateCalles the function
printFun
again. This is called recursion. There are many good tutorials on this and it's considered a programming fundamental by many.Basically, what is happening, printFun is being run again, but this time, the test paramenter is decreased by 1 . Any code below this line then waits for the function to execute again. Since
test
keeps decreasing by 1, eventually, it will be smaller than 1, and the function will terminate, which will cause the code beneath the function call to execute.Looking at the last two lines,
test
is set to three and the function is called with a 3.