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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ridolenai
  • 25
  • 1
  • 8
  • Possibly also related: [Python : name 'math' is not defined Error?](http://stackoverflow.com/q/8329601), if you used `from math import *`. – Martijn Pieters Feb 01 '15 at 03:27

1 Answers1

1

You have to import the math module first:

import math
math.factorial(x)
101
  • 8,514
  • 6
  • 43
  • 69
  • Is there another method with which to calculate a factorial progression without using the math.factorial(x) command? – Ridolenai Feb 01 '15 at 03:26
  • @Josh: [Function for Factorial in Python](http://stackoverflow.com/q/5136447) – Martijn Pieters Feb 01 '15 at 03:27
  • @Josh What's wrong with the `math` module? I'm always curious when I see requests to avoid using the standard library. – SethMMorton Feb 01 '15 at 03: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 01 '15 at 03: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 01 '15 at 03:35