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

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -