5. Factor - Wrong answer passed


Oops, try again. factorial(2) returned None, did you remember to return the result?

This answer below could not pass the exercise but have a correct answer, however, if i remove return ans, no answer showed but it passed. why does it happened?

def factorial(x):
    ans = 1
    if x == 1 or x == 0:
        return 1
    else :
        for n in range(1,x):
            ans = x*ans
            x = x-1
    return ans
    print ans    


The code you posted passes
Removing the second return statement causes it to not pass with the error message you pasted.

You're saying it's the other way around, are you sure your observations are correct?

Further, you said it doesn't show if you remove the return statement, but you have a print afterwards, so it would show

Nothing is said about showing though, so whether something is shown or not isn't necessarily an indication of whether the function behaves as it should.

Pretty sure the submission test is behaving correctly here. (As so happens, I wrote it)


I think this is a mistake from codeacademy and I will try to pass this by doing it the hard way. But if you try this it lets you pass lool:

import math
def factorial(x):
..... return math.factorial(x):


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.