2

Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it. For example: if num = 4, then your program should return (4 * 3 * 2 * 1) = 24. For the test cases, the range will be between 1 and 18 and the input will always be an integer.

and this is my code

def FirstFactorial(num):
    x = [1]
    if num == 1:
        return 1
    else:
        for i in range(1,num+1):
            x = x*(i)
    return x

print (FirstFactorial(4))

The expected output is 24. I get the following output from the code given above.

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
8
  • 3
    Why x = [1] rather than x = 1? Factorials are not lists. Commented Jun 13, 2019 at 23:03
  • What is the output of your code? Commented Jun 13, 2019 at 23:04
  • @moreON should be a very long list of 1s Commented Jun 13, 2019 at 23:05
  • 2
    @JohnColeman - sure would be, but surely it's nice to encourage people to include actual output in their questions? Commented Jun 13, 2019 at 23:07
  • @moreON Good point Commented Jun 13, 2019 at 23:08

5 Answers 5

6

Let's make a better code:

def factorial(num):
    f = 1
    for i in range(1, num+1):
        f = f * i
    return f

Some parts of your code have no sense at all, for example, x = [1] declares x equals a list with one element one. Then if you make list * number in python you multiply the list:

x = [1, 2]
x = x * 2
print(x) # prints [1, 2, 1, 2]

The if statement that checks if the number is 1 is not necessary using the code above.

Sign up to request clarification or add additional context in comments.

1 Comment

@MarcoMaher mark the answer as the correct one :) nice to know i helped
1

You could simply do:

import math
print(math.factorial(4))  

output:

24

Comments

0

You have done a small mistake. Instead of defining the variable x as a list (in the first line after the definition of the function FirstFactorial(num) , x=[1]) , you want do define x as a single number, i.e. x=1.

In Python, multiplication of a list by a positive scalar (say n) leads to repetition of the numbers in the list n number of times. In other words, the * operator repeats a list for the given number of times. This is what was happening in your code in the line x = x*(i).

You can read more about python lists here.

The following code will work fine, and do your job. The only change that I have done is in the first line after the definition of the function FirstFactorial(num).

def FirstFactorial(num):
    x = 1
    if num == 1:
        return 1
    else:
        for i in range(1,num+1):
            x = x*(i)   
    return x

print (FirstFactorial(4)) 
>>24

Also, you do not need to have a separate if statement to define 1!. The following code is more concise and it will give you factorials of all numbers including 1.

def FirstFactorial(num):
    x = 1
    for i in range(1,num+1):
        x = x*(i)   
    return x

print (FirstFactorial(1))
>> 1

Comments

0

You had error in your x declaration. Also, using lambda expression.

fact = lambda n:1 if n==0 else n*fact(n-1)

print(fact(4))

>>> 24

42 bytes

fact = lambda x:0**x or x*fact(x-1)

print(fact(4))

27 bytes

Python has C type internal implementation of factorial method in math module. Since it's C, it's faster than Python's approach.

import math

print(math.factorial(4))

First answer still has for loop,

def factorial(n):
    return 1 if ( n == 1 or n == 0 ) else n * factorial(n - 1)

2 Comments

Somewhat baroque (especially for a beginner like OP), plus: fact(1000) throws RecursionError: maximum recursion depth exceeded in comparison
You can increment stack depth for deeper recursive calls, sys.setrecursionlimit().
0

Actually correct one for me I got this and achieved to given test result.

def FirstFactorial(num):
  if num ==1:
    return 1
  else:
    for i in range(1, num-1/2):
       num = num*(i)
  return num

# keep this function call here 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.