-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 3 commits
bd96a91
7da46aa
648e170
9f75648
2a96cfc
ab332a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# factorial of a number | ||
|
||
|
||
n=int(input("Enter a number")) | ||
def factorial(n): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function should raise a ValueError just like math.factorial() does: % python3
|
||
for i in range(1,n+1): | ||
fact*=i | ||
return fact | ||
|
||
if n>=0: | ||
print(factorial(n)) | ||
else: | ||
raise ValueError("ValueError: factorial() not defined for negative vlaues") | ||
|
||
|
||
|
||
|
||
|
There was a problem hiding this comment.
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