+ 6

Need some help for assignment today

How to calculate the Factorial: Sample output: 0! = 1 1! = 1 2! = 1 3! = 1 . . n! = 1 * 2 * 2 * 3 * 2 * 3 * ... n-1 * n Please enter a number: 5 5! = 120 My attempt: # Write your code def factoRecurs(numb): if numb ==1: return 1 else: print(numb) return numb * factorRecurs(numb-1) inpNum = int (input(“Enter a number”)) if inpNum >= 1: print(“the factorial ”)

11th May 2020, 9:58 AM
Allll
47 Réponses
+ 14
You can use a loop, a recursive function and a few other ways. Try to figure out your own solution, only practice makes you a programmer. If you can't get it to run, you can link your attempt here, we'll try to help you with a hint instead of the answer.
11th May 2020, 10:53 AM
HonFu
HonFu - avatar
+ 8
A simple way is also to use a for loop with a range object.
11th May 2020, 10:51 AM
Lothar
Lothar - avatar
+ 3
Try this code... print((lambda x: 1 if x==0 else __import__('functools').reduce(lambda a,b:a*b, [i for i in range(1, x+1)])) (int(input())))
11th May 2020, 4:00 PM
Jenson Y
+ 3
use a for loop with a range object
13th May 2020, 7:53 AM
malek alshoubaki
malek alshoubaki - avatar
+ 1
i will try my best
11th May 2020, 10:54 AM
Allll
+ 1
i will thanks
11th May 2020, 4:10 PM
Allll
+ 1
Try this one..... def fact(x): a=1 for i in range (1,x): a*=i print('Factorial of',num,'is',a) num=int(input('Enter a number:')) if num==0: print('Factorial of',num,'is 1') elif num>0: fact(num+1) else: print('Invalid Entry')
12th May 2020, 10:58 AM
Sanjyot21
Sanjyot21 - avatar
12th May 2020, 4:31 PM
Shreyash Sinha
Shreyash Sinha - avatar
+ 1
n=int(input()) f=1 if (n==0): print("1") elif(n>0): while(n!=0): f=f*n n-=1 print(f)
13th May 2020, 9:12 AM
Deepti Verma
Deepti Verma - avatar
0
Your samples.are you sure, they are correct?
11th May 2020, 10:48 AM
GeorgeT
0
yeah i copy it from the assignment
11th May 2020, 10:49 AM
Allll
0
this is the question Write a function that accepts n as a parameter and calculate its factorial (n!) and return its result.
11th May 2020, 10:58 AM
Allll
0
Frankly, I can not find a solution
11th May 2020, 11:13 AM
Allll
0
and i need to solve it to pass
11th May 2020, 11:13 AM
Allll
0
# Write your code def factoRecurs(numb): if numb ==1: return 1 else: print(numb) return numb * factorRecurs(numb-1) inpNum = int (input(“Enter a number”)) if inpNum >= 1: print(“the factorial ”)
11th May 2020, 11:34 AM
Allll
0
can you try it and find the problem
11th May 2020, 11:35 AM
Allll
0
thanks so much but this solotion its okay with the rols in the question?
11th May 2020, 11:39 AM
Allll
0
not like this but my teacher are so intense with code he didnt accept any is my code okay with his rols?
11th May 2020, 11:44 AM
Allll
0
so i will get the full mark? i wish it
11th May 2020, 11:49 AM
Allll