"Python" programming language Introduction Classic 100 cases--26

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.