0

How does one use the factorial command in Python? I have attempted the math.factorial command, and either I am using it completely incorrectly, or it isn't working and returns the error

NameError: name 'math' is not defined

for some reason.

1

1 Answer 1

1

You have to import the math module first:

import math
math.factorial(x)
5
  • Is there another method with which to calculate a factorial progression without using the math.factorial(x) command?
    – Ridolenai
    Feb 1, 2015 at 3:26
  • @Josh: Function for Factorial in Python
    – Martijn Pieters
    Feb 1, 2015 at 3:27
  • @Josh What's wrong with the math module? I'm always curious when I see requests to avoid using the standard library. Feb 1, 2015 at 3:31
  • 1
    @SethMMorton If I'm being honest, it would be because I am learning Python in a Comp Sci class and it hasn't been covered so I'm a bit on the fence about whether to use it or not. I was trying to figure out how to complete it using commands that we were taught, but have since figured out that I have these 'textbooks' and 'useful links' that were provided by the professor for a reason. That is why I was attempting to avoid the standard library.
    – Ridolenai
    Feb 1, 2015 at 3:34
  • @MartijnPieters If you look at the post above, you will notice the reason why I was unable to put together the asin() post that you referred to previously as having already answered this question as being related. That being said, I am truly appreciative of everything that you two have explained.
    – Ridolenai
    Feb 1, 2015 at 3:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.