简体   繁体   English

为什么Python显示错误:无法分配给运营商?

[英]Why is Python showing error : can't assign to operator?

from datetime import datetime
now = datetime.now()
q = int(now.strftime('%I'))
w = int(now.strftime('%M'))

def correct_time(delta):
    if w < (120 - delta):
        Q = q + 1 and W = w + (delta - 60)
    else:
        Q = q + 2 and W = w + (delta - 120)
    return Q, W

correct_time(45)

#gives error - SyntaxError: can't assign to operator

In the above code I was trying to assign new values of q and w to Q and W respectively. 在上面的代码中,我试图分别将q和w的新值分配给Q和W. I get no error when I only try to assign new value to either Q or W (not to both in the same line) but when I use 'and'(as in the above case) or ',' I get such an error. 当我只尝试为Q或W(不是同一行中的两个)分配新值时,我得到没有错误,但当我使用'和'(如上例所示)或','时,我得到这样的错误。

I searched the internet for 'can't assign to operator' error but found that they were not relevant to my case. 我在互联网上搜索“无法分配给操作员”错误,但发现它们与我的案例无关。 I would like to know why the above code shows error when it follows the rules of assignment. 我想知道为什么上面的代码在遵循赋值规则时会显示错误。

We all know that only expression and expression is valid. 我们都知道只有expression and expression是有效的。 So the point here is that in Python, assignment is just a statement , not expression . 所以这里的重点是,在Python中, assignment只是一个statement ,而不是expression In C , a = 3 is assignment expression and a = 3 && b = 4 is valid. Ca = 3assignment expressiona = 3 && b = 4是有效的。 But, this is not valid in Python. 但是,这在Python中无效。

And Python will have its own assignment expression in 3.8: PEP 572 -- Assignment Expressions | Python将在3.8中有自己的assignment expressionPEP 572 - 赋值表达式 Python.org Python.org

In Python3.8, you will be able to do such thing: 在Python3.8中,您将能够执行以下操作:

# This block code only works with Python3.8+
from datetime import datetime
now = datetime.now()
q = int(now.strftime('%I'))
w = int(now.strftime('%M'))

def correct_time(delta):
    if w < (120 - delta):
        (Q := q + 1) and (W := w + (delta - 60))
    else:
        (Q := q + 2) and (W := w + (delta - 120))
    return Q, W

correct_time(45)

But, it is not necessary, since you can just do such thing in Python: 但是,没有必要,因为你可以在Python中做这样的事情:

a, b = 1, 2

which equals to 等于

a = 1
b = 2

Your code should be: 你的代码应该是:

def correct_time(delta):
    if w < (120 - delta):
        Q = q + 1
        W = w + (delta - 60)
    else:
        Q = q + 2
        W = w + (delta - 120)
    return Q, W

In an expression like a = 1 and b = 2 , Python would first try to assign the value 2 to a and b , which is impossible and produces the error SyntaxError: can't assign to operator . a = 1 and b = 2的表达式中,Python首先尝试将值2分配给a and b ,这是不可能的,并产生错误SyntaxError: can't assign to operator You would get something similar with a = 1 = 2 : here, trying to assign 2 to 1 would produce SyntaxError: can't assign to literal . 你会得到类似a = 1 = 2 :在这里,尝试分配21会产生SyntaxError: can't assign to literal

What you're looking for is the following syntax: 您正在寻找的是以下语法:

def correct_time(delta):
    if w < (120 - delta):
        Q, W = q + 1, w + (delta - 60)
    else:
        Q, W = q + 2, w + (delta - 120)
    return Q, W

In your case it doesn't work because right hand should be expression but it's expression and statement (due to assignment). 在你的情况下,它不起作用,因为右手应该是表达式,但它的expressionstatement (由于赋值)。

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

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