0
def factorial(num):
    fac=1
    if num!=0:
        fac= ((fac*i) for i in range(1,number+1)) 
    return fac
print ("Enter the number:")
number =int(input())
print (str(factorial(number)))

''' comprehension is not allowed in line 4. Why so? is that a limitation? '''

smohamm7
  • 9
  • 3
  • 1
    it's not a limitation but error with your code. Your comprehension should be using `num` instead of `number`. PS: There are few more errors in your code. But as far as your question is concerned, no, there is no limitation. You can use it, *whether it should be used there is a different question* – Moinuddin Quadri Feb 16 '18 at 00:12
  • It's not a comprehension it's a [generator expression.](https://www.python.org/dev/peps/pep-0289/) Who told you it was a comprehension? – Peter Wood Feb 16 '18 at 00:19
  • @Quadri-I learnt its a generator and I am new to the lang. So still coping. But the parameterand variableshould not matter as long as they are of same data type. Let me know if I'm wrong. ;) – smohamm7 Feb 16 '18 at 14:22
  • @PeterWood-Thx for the info. Looking into generator expression. – smohamm7 Feb 16 '18 at 14:23

1 Answers1

0

Try something like this if your goal is to calculate a factorial

for i in range(2,num+1):
    fac = fac * i 

If your goal is to generate a list up to N factorial using list comprehension then check out this post : Create list of factorials using list comprehension

Mitch
  • 3,342
  • 2
  • 21
  • 31