How to assign a variable in a condensed if-else statement