生信

python unit 4 creat a factorial

2018-08-21  本文已影响0人  我最有才

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:

上一篇下一篇

猜你喜欢

热点阅读