5.15 Factorial Function


On most of the posts for this lesson I have seen that most users will code an entire factorial function for Python. To save everyone the time and effort, I'd like to point out that math.factorial() is a Python function and can be used in this exercise. I did "import math" at the beginning of my code, I'm not sure if that was necessary, but my code passed all the same. Happy coding to you!


Did you learn by having a built in function handed to you on a platter, or by building working algorithms?


from math import factorial

would have worked, too. Yes, you did need to import from the math module, and that is not covered in this lesson. Should learners discover it on their own (as we would hope), it is not a triumph that needs to be publicized.


I'm sorry if I can across as proud, it wasn't my intention. For your information, I did write out the algorithm, because I am trying to learn Python well. I also think that part of learning a programming language well learning to utilise it to its full potential. One of the beauties of Python is that one doesn't have to "reinvent the wheel" for most common functions. In the lesson it says to "Consider having factorial() call itself.", it seemed to me (perhaps in error) that factorial() itself was being treated as a Python function, not as a function to be written. It was not my intention to "cut corners", and certainly not to boast.


well, factorial() doesn't exist, until you import that function from Math or create it yourself.
that's the beauty of programming in general, there's no one way of solving a problem, for most situations.
you can import functions you need, or make them yourself.