我在 python 中的阶乘代码有什么问题

编程入门 行业动态 更新时间:2024-10-27 12:33:05
本文介绍了我在 python 中的阶乘代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码用于在 python 中计算数字的阶乘.但我不明白为什么我得到的答案是 1.有人可以纠正我的代码.我想在不使用递归的情况下计算阶乘.

I have the following code for calculating the factorial of a number in python. but I couldnt understand why I am getting the answer as 1. can some one correct my code. I want to calculate the factorial without using recursion.

def factorial (n): result =1 num = n while n<1: result = result * num num = num -1 return result factorial(5) 1

推荐答案

while n < 1:

应该是

while num > 1:

更多推荐

我在 python 中的阶乘代码有什么问题

本文发布于:2023-11-29 19:41:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647437.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阶乘   有什么   我在   代码   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!