Python 阶乘实例

Python 3 教程 · 2019-02-12 11:56:57

整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,0的阶乘为1。即:n!=1×2×3×...×n。

实例

#!/usr/bin/python3 # Filename : test.py # author by : www.codercto.com # 通过用户输入数字计算阶乘 # 获取用户输入的数字 num = int(input("请输入一个数字: ")) factorial = 1 # 查看数字是负数,0 或 正数 if num < 0: print("抱歉,负数没有阶乘") elif num == 0: print("0 的阶乘为 1") else: for i in range(1,num + 1): factorial = factorial*i print("%d 的阶乘为 %d" %(num,factorial))

执行以上代码输出结果为:

请输入一个数字: 3
3 的阶乘为 6

点击查看所有 Python 3 教程 文章: https://www.codercto.com/courses/l/10.html

查看所有标签

The Apache Modules Book

The Apache Modules Book

Nick Kew / Prentice Hall PTR / 2007-02-05 / USD 54.99

"Do you learn best by example and experimentation? This book is ideal. Have your favorite editor and compiler ready-you'll encounter example code you'll want to try right away. You've picked the right......一起来看看 《The Apache Modules Book》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具