用while循环python写阶乘 [英] Write factorial with while loop python

查看:68
本文介绍了用while循环python写阶乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new and do not know a lot about Python. Does anybody know how you can write a factorial in a while loop?

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

I can make it in an if / elif else statement:

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 循环(没有函数)来做到这一点.

But I want to do this with a while loop (no function).

推荐答案

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

这篇关于用while循环python写阶乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆