python - What is wrong with this ternary operator? -


for in str1:     (newstr += chr(ord(i)+2)) if i.isalpha() else (newstr += i) 

it seems grieving += operator. know both variables strings though, don't understand why not concatenate them

try following:

for in str1:     newstr += (chr(ord(i)+2) if i.isalpha() else i) 

edit:

from python documentation:

conditional_expression ::=  or_test ["if" or_test "else" expression] expression             ::=  conditional_expression | lambda_expr 

and pointed @flornquake, assignment var += value statement, not expression.


Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -