📄 claude/resources/prompt-library/python-bug-buster

File: python-bug-buster.md | Updated: 11/15/2025

Source: https://docs.claude.com/en/resources/prompt-library/python-bug-buster

Agent Skills are now available! Learn more about extending Claude's capabilities with Agent Skills .

Claude Docs home pagelight logodark logo

US

English

Search...

Ctrl K

Search...

Navigation

Prompt Library

Python bug buster

Home Developer Guide API Reference Model Context Protocol (MCP) Resources Release Notes

On this page

Copy this prompt into our developer Console to try it for yourself!

| | 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): <br>sum = 0 <br>for num in nums: <br>sum += num <br>average = sum / len(nums) <br>return average <br> <br>numbers = [10, 20, 30, 40, 50] <br>result = calculate_average(numbers) <br>print(“The average is:”, results) |

Example Output

Corrected Python code:

Python

Copy

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

  • Python
  • TypeScript
  • AWS Bedrock Python
  • AWS Bedrock TypeScript
  • Vertex AI Python
  • Vertex AI TypeScript

Copy

import anthropic

client = anthropic.Anthropic(
  # defaults to os.environ.get("ANTHROPIC_API_KEY")
  api_key="my_api_key",
)
message = client.messages.create(
  model="claude-sonnet-4-5",
  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": [\
        {\
          "type": "text",\
          "text": "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)

Was this page helpful?

YesNo

Google apps scripter Time travel consultant

Assistant

Responses are generated using AI and may contain mistakes.