Skip to content

Create factorial_iterative.py #1693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions maths/factorial_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@


n=int(input("Enter a number"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will fail our automated testing unless it is indented under if __name__ == "__main__": as discussed in CONTRIBUTING.md


def factorial(n):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other functions in this repo to see how type hints and doctests work. They are required.

fact=1
Copy link
Member

@cclauss cclauss Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function should raise a ValueError just like math.factorial() does:

% python3

>>> import math
>>> math.factorial(-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: factorial() not defined for negative values

for i in range(1,n+1):
fact*=i
return fact

print(factorial(n))
if n>=0:
print(factorial(n))
else:
print("number should be positive")