Mina - 1 year ago 63
Javascript Question

# Why line break breaking python code

We're sorry if it sounds too noob. But this is our life's first encounter with Python.

We have got a python function like this:

``````def m(n,k,c=0):x,y=n;return c if n==k else 7*(c>6)or min(m((x+a,y+b),k,c+1)for a,b in[(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)])
``````

which is being called like this:

``````print m((1,2), (5,6))
``````

In order to understand it we were trying to add line breaks like this:

``````def m(n,k,c=0):x,y=n;return c
if n==k else 7*(c>6) or min(m((x+a,y+b),k,c+1)for a,b in[(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)])
``````

We're not understanding why is just a simple new line breaking the code

Once we understand this, probably we shall be able to convert this program to javascript:

``````function m(n, k, c = 0) {
x,
y = n;
return c
if (n == k) {

} else {
7 * (c > 6) or Math.min(m((x + a, y + b), k, c + 1) for a, b in [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)])
}
}``````

When you define a function, either the function definition must be all on the same line (as in your original example), or the header `def m(n,k,c=0):` must be on the line of its own, and the remaining statements must be on the next line(s). You cannot mix-and-match.

``````def m(n, k, c=0):
x, y = n
return c if n==k else \
7 * (c > 6) or min(m((x + a, y + b), k, c + 1)
for a,b in [(1,2), (1,-2), (-1,2), (-1,-2),
(2,1), (2,-1), (-2,1), (-2,-1)])

m((1,2), 3)
#7
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download