简体   繁体   English

如何将此 function 从 javascript 转换为 python?

[英]How to convert this function from javascript to python?

Does anybody know how to convert this javascript function to python?有人知道如何将这个 javascript function 转换为 python 吗?

javascript: javascript:

function ding(t, a, e, n) {
  return t > a && t <= e && (t += n % (e - a)) > e && (t = t - e + a), t
}

This is my try on doing so:这是我这样做的尝试:

def ding(t, a, e, n):
    return t > a and t <= e and (t + n % (e - a)) > e and (t = (t - e + a)), t

It returns a syntax error at the "=" in (t = (t - e + a)) and idk how to solve this right.它在(t = (t - e + a))中的“=”处返回语法错误,并且知道如何解决这个问题。

When giving it these values: ding(53, 47, 57, 97) it should return 50 in the original javascript function.当给它这些值时: ding(53, 47, 57, 97)它应该在原始 javascript function 中返回 50。

Does it have to be a one-liner?一定要单排吗? Why not just split it into a few lines:为什么不把它分成几行:

def ding(t, a, e, n):
    if t > a and t <= e:
        t += n % (e - a)

        if t > e:
            t -= e - a
    
    return t
    
print(ding(53, 47, 57, 97)) # 50

that is because python doesn't support evaluating value assignment as assigned value.那是因为 python 不支持将赋值赋值为赋值。 (t = (t - e + a)) causes SyntaxError instead of returning (t - e + a) . (t = (t - e + a))导致 SyntaxError 而不是返回(t - e + a)

&& operators on the original code seems to be used to eliminate the usage of if operators.原始代码上的 && 运算符似乎用于消除 if 运算符的使用。 This should behave the same way as it did on JS.这应该与在 JS 上的行为方式相同。

def ding(t, a, e, n):
    if a < t <= e:
        t += n % (e - a)
        if t > e:
            t -= (e - a)
    return t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM