简体   繁体   中英

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. So the correct way to write this is not to repeat the assignment:

model_gender = '' if model_props.gender == 'MALE' else ' [F]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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