[英]Can't assign to conditional expression syntax error
Any idea what am I doing wrong into this one line if/else conditional expression?知道如果/其他条件表达式在这一行中我做错了什么吗?
model_gender = '' if model_props.gender == 'MALE' else model_gender = ' [F]'
I get this error:我收到此错误:
model_gender = '' if model_props.gender == 'MALE' else model_gender = ' [F]'
^
SyntaxError: can't assign to conditional expression
model_gender = '' if model_props.gender == 'MALE' else model_gender = ' [F]'
^
the error should have pointed here instead
The syntax for a conditional expression is A if C else B
, which is weird and different from your usual if
statement.条件表达式的语法是A if C else B
,这很奇怪,与您通常的if
语句不同。 So the correct way to write this is not to repeat the assignment:所以正确的写法是不要重复赋值:
model_gender = '' if model_props.gender == 'MALE' else ' [F]'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.