gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
give me an example of a python code
- User
give me an example of a python code
- Assistant
Certainly! Here’s a simple example of a Python script that calculates the factorial of a number using a function.
def factorial(n): """Calculate the factorial of a non-negative integer n.""" if n < 0: return "Factorial is not defined for negative numbers." elif n == 0 or n == 1: return 1 else: result = 1 for i in range(2, n + 1): result *= i return result # Ask the user for a number number = int(input("Enter a non-negative integer: ")) # Calculate and print the factorial print(f"The factorial of {number} is {factorial(number)}.")This script defines a function called
factorialthat computes the factorial of a non-negative integer. It uses a loop to multiply numbers from 2 ton, handling special cases for negative numbers and zero. The user is prompted to enter a number, and the script calculates and prints the factorial of that number.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.