python unit 4 creat a factorial and creat a you own factorial function

When we want to creat a factoral ,the for loops can do you a favor.Now let`s do it.

a=int(input("whatever numbers do you want to calculate"))   or  a= eval( input())  

b=1

for i in range(a):

b=b*(i+1)

print(b)

#####Notice: the most important is 1*2*3*4*******a   so easy?

And if you want to use this factorial for more than one time and you donn`t want to repeat this program again again ,then you can define your own function,just like the function inside the python.  

def factorial(numbers):

x=1

for i in range(numbers):

x=x*(i+1)

return(x)

#$$Then you can use your own function now

y=eval(input())    or y=int(input())  

z=factorial(y)

print(z)


or use 6*5*4*3*2*1

# define a factoral function

def factorial(numbers):

    if numbers<=1:

      return 1

    else:

        return numbers*factorial(numbers-1)

y=eval(input())

z=factorial(y)

print(z)



Then i want to do this analysing in resusive program :

The following:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容