Chat
Files
Terminal
Preview

How can I help you today?

Ask me anything or try one of these examples

Write a Python function
Create a function to calculate Fibonacci sequence
Creative story
Write a short story about a robot learning emotions
Data analysis
Help me analyze sales data and identify trends
Translate text
Translate this paragraph to Spanish
U
You
Hello! Can you help me write a Python function to calculate the factorial of a number?
AI
Assistant
Thinking about the best approach...

Sure! Here's a Python function to calculate the factorial of a number:

Python
def factorial(n):
    """
    Calculate the factorial of a non-negative integer.
    
    Args:
        n (int): A non-negative integer
    
    Returns:
        int: The factorial of n
    """
    if n < 0:
        raise ValueError("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

# Example usage
print(factorial(5))  # Output: 120

This function handles edge cases and includes proper documentation. Would you like me to explain any part of it or create a recursive version instead?

Projects
web_app.py
README.md
design.png
Documents
report.pdf
proposal.docx
notes.txt
Terminal
[user@ai-assistant ~]$ ls -la
total 48
drwxr-xr-x 12 user staff 384 Jun 10 10:30 .
drwxr-xr-x 5 user staff 160 Jun 5 14:22 ..
-rw-r--r-- 1 user staff 324 Jun 8 09:15 .gitignore
drwxr-xr-x 4 user staff 128 Jun 10 10:30 src
drwxr-xr-x 3 user staff 96 Jun 9 16:45 docs
-rw-r--r-- 1 user staff 1024 Jun 10 10:28 package.json
[user@ai-assistant ~]$
[user@ai-assistant ~]$
Preview
Press Enter to send, Shift+Enter for new line