GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add n! · TUGOU321/StarterLearningPython@786bb9f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 129.md
  • 1code
    • 12905.py
    • 12905_2.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎129.md‎

Copy file name to clipboardExpand all lines: 129.md
+38-1Lines changed: 38 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,45 @@ OK!完美地解决了问题,去除了code前面的一个空格。
190190

191191
保存运行之,看看结果和你推算的是否一致。
192192

193+
##练习5
194+
195+
**问题描述**
196+
197+
在数学中,用n!来表示阶乘。例如,4!=1×2×3×4=24。如果将n个物体排列,有多少种排列方式,那就是n!。根据定义,0!=1。
198+
199+
**解析**
200+
201+
下面用Python程序来计算阶乘。
202+
203+
#!/usr/bin/env python
204+
# coding=utf-8
205+
206+
n = int(raw_input("Enter an interger >=0: "))
207+
208+
fact = 1
209+
210+
for i in range(2, n + 1):
211+
fact = fact * i
212+
213+
print str(n) + " factorial is " + str(fact)
214+
215+
这是用for循环来实现的,当然,你也可以使用while循环来计算阶乘。其代码如下:
216+
217+
#!/usr/bin/env python
218+
# coding=utf-8
219+
220+
n = int(raw_input('Enter an integer >= 0: '))
221+
222+
fact = 1
223+
i = 2
224+
while i<=n:
225+
fact = fact * i
226+
i += 1
227+
228+
print str(n) + " factorial is " + str(fact)
229+
193230
------
194231

195232
[总目录](./index.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[上节:迭代](./128.md)&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;[下节:自省](./130.md)
196233

197-
如果你认为有必要打赏我,请通过支付宝:**[email protected]**,不胜感激。
234+
如果你认为有必要打赏我,请通过支付宝:**[email protected]**,微信号:***qiwsir*不胜感激。
Collapse file

‎1code/12905.py‎

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
n = int(raw_input("Enter an interger >=0: "))
5+
6+
fact = 1
7+
8+
for i in range(2, n + 1):
9+
fact = fact * i
10+
11+
print str(n) + " factorial is " + str(fact)
Collapse file

‎1code/12905_2.py‎

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
n = int(raw_input('Enter an integer >= 0: '))
5+
6+
fact = 1
7+
i = 2
8+
while i<=n:
9+
fact = fact * i
10+
i += 1
11+
12+
print str(n) + " factorial is " + str(fact)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.