-1

can someone explain what's wrong with the below code?

     def factorial(num):
        if num == 0 or 1:
           return 1
        else:
           result = num * factorial(num-1)
           return result

#print(factorial(30))
#output: 1

& on the contrary when the if statement is slightly modified as below. The code seems to do what its meant to do. I would appreciate if someone could comment on why the code below provides desired output while the code above doesn't

     def factorial(num):
        if num == 1 or 0:
           return 1
        else:
           result = num * factorial(num-1)
           return result
 #print(factorial(30))
 #265252859812191058636308480000000
reddy911
  • 1
  • 4
Sahas
  • 259
  • 2
  • 12

1 Answers1

0

num == 0 or 1 isn't doing what you think it is.

This could instead be num == 0 or num == 1 or num in [0,1].

samwalton
  • 521
  • 4
  • 13