1 #题目: Use recursive method to find 5!.
Code:
2 def FAC (n): 3 if n = = 1:4 return 1 5 return N*FAC (n-1) 6 print (' 5 factorial is%d '%FAC (5))
Operation Result:
[[email protected] code_100]# Python code_26.py 5 factorial is 120[[email protected] code_100]#
Code Explanation:
Idea: Recursion generally uses the form of a function to invoke the function itself in a function, after reaching a condition that ends the function 2 def FAC (n): #定义一个函数 3 if n = = 1: #当 The parameter n equals 1 o'clock, that is, the factorial of recursion to 1 4 return 1 #结束递归, returns 1 5 return N*FAC (n-1) #当n >1 recursive operation, and return Result 6 print (the factorial of ' 5 is%d '%FAC (5)) #调用函数fac, the factorial value when n=5 is printed
This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1748746
Python programming language Introduction Classic 100 example--26