# factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorialdef factorial(n: int) -> int:""">>> import math>>> all(factorial(i) == math.factorial(i) for i in range(20))True>>> factorial(0.1)Traceback (most recent call last):...ValueError: factorial() only accepts integral values>>> factorial(-1)Traceback (most recent call last):...ValueError: factorial() not defined for negative values"""if n != int(n):raise ValueError("factorial() only accepts integral values")if n < 0:raise ValueError("factorial() not defined for negative values")value = 1for i in range(1, n + 1):value *= ireturn valueif __name__ == "__main__":n = int(input("Enter a positive integer: ").strip() or 0)print(f"factorial{n} is {factorial(n)}")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。