Skip to content

Instantly share code, notes, and snippets.

@iMel408
Created March 24, 2019 18:52
Show Gist options
  • Save iMel408/828d503a0efd668b385ad2297da3b123 to your computer and use it in GitHub Desktop.
Save iMel408/828d503a0efd668b385ad2297da3b123 to your computer and use it in GitHub Desktop.
calculate factorial of a given pos int. #practice
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