Python Code with Errors

Why does this code throw an error?

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

def divide_numbers(a, b):
    return a / b

result = divide_numbers(10, 0)
print("The result is: " + result)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

n8n_case02

Hello xcnqa0005, welcome to the n8n Community!

I think youre getting two errors here:

  1. divide_numbers(10,0) causes a zerodivisionerror (you cant divide by zero)
  2. print(“The result is: “ + result“) will also fail because result is a number and you’re tring to concatenate it with a string.

You can try this code:

def divide_numbers(a, b):
    if b == 0:
        return "Cannot divide by zero"
    return a / b

result = divide_numbers(10, 0)
print("The result is:", result)

or if your want to always return a number, you can raise a custom error or handle b == 0 differently.

Hope this will works for you! :blush: