Skip to content

Python bug buster (Claude Prompt Sample)

    Detect and fix bugs in Python code.

    Content
    System
    Your task is to analyze the provided Python code snippet, identify any bugs or errors present, and provide a corrected version of the code that resolves these issues. Explain the problems you found in the original code and how your fixes address them. The corrected code should be functional, efficient, and adhere to best practices in Python programming.
    User
    def calculate_average(nums):
        sum = 0
        for num in nums:
            sum += num
        average = sum / len(nums)
        return average
    
    numbers = [10, 20, 30, 40, 50]
    result = calculate_average(numbers)
    print("The average is:", results)

    Example output

    > def calculate_average(nums):  
    >     total = 0  
    >     for num in nums:  
    >         total += num  
    >     average = total / len(nums)  
    >     return average
    >
    > numbers = [10, 20, 30, 40, 50]  
    > result = calculate_average(numbers)  
    > print("The average is:", result)

    Explanation:

    1. Changed the variable name “sum” to “total” to avoid using the built-in Python function “sum()”.
    2. Fixed the typo in the print statement, changing “results” to “result” to correctly reference the variable.

    API request

    PythonTypeScript
    import anthropic
    
    client = anthropic.Anthropic(
        # defaults to os.environ.get("ANTHROPIC_API_KEY")
        api_key="my_api_key",
    )
    message = client.messages.create(
        model="claude-3-opus-20240229",
        max_tokens=1000,
        temperature=0,
        system="Your task is to analyze the provided Python code snippet, identify any bugs or errors present, and provide a corrected version of the code that resolves these issues. Explain the problems you found in the original code and how your fixes address them. The corrected code should be functional, efficient, and adhere to best practices in Python programming.",
        messages=[
            {"role": "user", "content": "def calculate_average(nums):  \n    sum = 0  \n    for num in nums:  \n        sum += num  \n    average = sum / len(nums)  \n    return average  \n  \nnumbers = [10, 20, 30, 40, 50]  \nresult = calculate_average(numbers)  \nprint(\"The average is:\", results)"}
        ]
    )
    print(message.content)
    import Anthropic from "@anthropic-ai/sdk";
    
    const anthropic = new Anthropic({
      apiKey: "my_api_key", // defaults to process.env["ANTHROPIC_API_KEY"]
    });
    
    const msg = await anthropic.messages.create({
      model: "claude-3-opus-20240229",
      max_tokens: 1000,
      temperature: 0,
      system: "Your task is to analyze the provided Python code snippet, identify any bugs or errors present, and provide a corrected version of the code that resolves these issues. Explain the problems you found in the original code and how your fixes address them. The corrected code should be functional, efficient, and adhere to best practices in Python programming.",
      messages: [
        {"role": "user", "content": "def calculate_average(nums):  \n    sum = 0  \n    for num in nums:  \n        sum += num  \n    average = sum / len(nums)  \n    return average  \n  \nnumbers = [10, 20, 30, 40, 50]  \nresult = calculate_average(numbers)  \nprint(\"The average is:\", results)"}
      ]
    });
    console.log(msg);

    Source: