python - 用while循环python写阶乘

标签 python loops while-loop factorial

我是新手,对 Python 了解不多。有人知道如何在 while 循环中编写阶乘吗?

我可以在 if/elif else 语句中实现:

num = ...
factorial = 1

if num < 0:
   print("must be positive")
elif num == 0:
   print("factorial = 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print(num, factorial)

但我想用一个 while 循环(无功能)来做到这一点。

最佳答案

while num > 1:
    factorial = factorial * num
    num = num - 1

关于python - 用while循环python写阶乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467303/

相关文章:

python - pymssql.OperationalError : DB-Lib error message 20009, 严重性 9

javascript - while 循环中激活函数时出现问题

python - 我正在努力让我的 tkinter 窗口在 while 循环中关闭

java - 什么是 NumberFormatException 以及如何修复它?

python - 关系 "background_task"不存在

python - 在 Python 中使用 Pygame 移动 Sprite

python - 如何在 SendKey 和 PywinAuto 模块中使用 Python 发送 TAB 按钮

JQuery:循环输入元素

javascript 循环进入心理状态

java - Java 中 while 和 for 的组合