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:
Hello xcnqa0005, welcome to the n8n Community!
I think youre getting two errors here:
- divide_numbers(10,0) causes a zerodivisionerror (you cant divide by zero)
- 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! 