fact_main.py
#!/usr/bin/env python3 import factorial from factorial import * def main(): F = FACTORIAL(10) F.factorial_math() F.factorial_python() F = FACTORIAL(-1) F.factorial_math() F.factorial_python() if __name__ == '__main__': main()
factorial.py
#!/usr/bin/env python3 import math class FACTORIAL: def __init__(self, num): self.num = num def factorial_math(self): print("factorial_math") j = 1 if self.num < 0: print("{}: You can't use a negative number".format(self.num)) print("") elif self.num == 0 | self.num == 1: print("{}".format(j)) print("") elif self.num > 0: for i in range(1, self.num + 1): j = j * i if j > 1: print("{}! (factorial) is: {}".format(self.num, j)) print("") def factorial_python(self): print("factorial_python") if self.num < 0: print("{}: You can't use a negative number".format(self.num)) print("") if self.num > 0: print("{}! (factorial) is {}".format(self.num, math.factorial(int(self.num)))) print("")
output
factorial_math 10! (factorial) is: 3628800 factorial_python 10! (factorial) is 3628800 factorial_math -1: You can't use a negative number factorial_python -1: You can't use a negative number Process finished with exit code 0