reka-core-20240722· unknown
LMArena Human Preference 100K
make me a simple python program with documentation
- User
make me a simple python program with documentation
- Assistant
Certainly! Below is a simple Python program that calculates the factorial of a number entered by the user. It includes detailed documentation using docstrings to explain each part of the program.
""" Factorial Calculator This program calculates the factorial of a non-negative integer entered by the user. Factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. Author: Your Name Date: Today's Date """ def factorial(n: int) -> int: """ Calculate the factorial of a non-negative integer. Args: n (int): A non-negative integer for which to calculate the factorial. Returns: int: The factorial of the input integer n. Returns 1 if n is 0. Raises: ValueError: If the input n is a negative integer. """ if n < 0: raise ValueError("Factorial is not defined for negative numbers.") elif n == 0: return 1 else: result = 1 for i in range(1, n + 1): result *= i return result def main(): """ Main function to run the program. Prompts the user for input, calculates the factorial, and displays the result. """ try: number = int(input("Enter a non-negative integer to calculate its factorial: ")) fact = factorial(number) print(f"The factorial of {number} is {fact}.") except ValueError as e: print(e) if __name__ == "__main__": """ Entry point of the program. Calls the main function to start the program. """ main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.