Created
March 24, 2019 18:52
-
-
Save iMel408/828d503a0efd668b385ad2297da3b123 to your computer and use it in GitHub Desktop.
calculate factorial of a given pos int.
#practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calc_factorial(n): | |
""" calculate factorial of a given positive int""" | |
x = 1 | |
if n < 0: | |
return 'error: param was neg int' | |
elif n == 1: | |
return(1) | |
else: | |
while n > 1: | |
x *= n | |
n -= 1 | |
return x | |
print("Result:", calc_factorial(5)) | |
print() | |
print('version w/ print statements through iteration:') | |
def calc_factorial_prnts(n): | |
""" calculate factorial of a given positive int""" | |
x = 1 | |
if n < 0: | |
return 'error: param was neg int' | |
elif n == 1: | |
return(1) | |
else: | |
while n > 1: | |
print("n:",n) | |
print("x:",x) | |
x *= n | |
print("x *= n:",x) | |
n -= 1 | |
return x | |
print("Result:",calc_factorial_prnts(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment