相关疑难解决方法(0)

使用Python进行因子计算

我是Python 新手,目前正在阅读Python 3的绝对初学者和面对以下问题.

我想用程序计算阶乘.

  1. 请求用户输入非负数n
  2. 然后使用for循环来计算阶乘

而代码是这样的:

N = input("Please input factorial you would like to calculate: ")
ans = 1
for i in range(1,N+1,1):
    ans = ans*i
print(ans)
Run Code Online (Sandbox Code Playgroud)

而我想添加一项功能来检查输入数字N是否为非负数.喜欢:

if N != int(N) and N < 0:
Run Code Online (Sandbox Code Playgroud)

我希望用户再次输入N,如果它不是非负数.

谢谢你的温柔帮助.

python factorial python-3.x

6
推荐指数
1
解决办法
5251
查看次数

标签 统计

factorial ×1

python ×1

python-3.x ×1