Writing a very simply function to mask all but the last 4 digits of a string with "#" characters. This is what I have so far:
def maskify(cc):
res = "#" * (len(cc) - 4) if len(cc) > 4 else return cc
res += cc[len(cc) - 4:]
return res
print(maskify("12355644"))
If I write out the ifs as statements, instead of using them as ternary operators, the function works fine. If I try to do the above, I get an invalid syntax error on res = "#" * (len(cc) - 4) if len(cc) > 4 else return cc The carat is pointing to the n in return. If I rewrite the above line to exclude the else part, then the carat points to the > 4.
What am I missing here? The program works fine using a traditional if-else method, but with the ternary all I'm seeing is an expression. Replacing len(cc) with a variable doesn't change anything either.
returnstatement in a conditional expressionreturn x if cond else yif you can figure out how to rewrite your function as such.returnto do there?returnis not an expression, and cannot be made part of other expressions.ccis less than 4 characters perhaps all characters should be masked otherwise the whole value would be known.