Python例子
- Python例子-图片验证码
- Python例子-将一个list的值加入字典,然后再插入到一个空列表
- Python例子-爬取糗事百科首页内容
- Python例子-urllib模拟游览器
- Python例子-读取远程页面并写入本地页面
- Python例子-通过装饰器实现已有函数执行时间的统计
- Python例子-使用闭包函数输出不同人的对话
- Python例子-面向对象(烤地瓜)
- Python例子-复制文件
- Python例子-计算阶乘 n! = 1 * 2 * 3 * ... * n
- Python例子-8个老师分配到3个办公室
- Python例子-求1~100中平方根是整数的数
- Python例子-求100-999之间所有的"水仙花数"
- Python例子-1、2、3、4组成互不相同不重复的三位数
- Python例子-年份是否是闰年
- Python例子-设计“过7游戏”
- Python例子-九九乘法表
- Python例子-统计字符串中,各个字符的个数
- Python例子-测试身材是否标准
- Python例子-计算1~100的累加和
- Python例子-猜拳游戏
- Python例子上公交车有座位可以坐下
Python例子-计算阶乘 n! = 1 * 2 * 3 * ... * n
Python例子-计算阶乘 n! = 1 * 2 * 3 * ... * n
计算阶乘 n! = 1 * 2 * 3 * ... *
两种方法,一种是用while循环实现,另一种是用递归函数实现
#while 方法 def cal_num(num): i = 1 result = 1 while i <= num: result *= i i += 1 return result res = cal_num(3) print(res) # 结果为6
# 递归函数方法 def cal_num(num): if num >= 1: result = num * cal_num(num-1) else: result = 1 return result res = cal_num(3) print(res) # 结果是6
最后修改:2020年2月27日 23:44