I'm trying to condense an if-elif-else statement into one line. I tried:
a == 1 ? print "one" : a == 2 ? print "two" : print "none"
But I got a syntax-error. I have also tried:
print "one" if a == 1 else print "two" if a == 2 else print "none"
But I also got a syntax-error.
What can I do to make any of these answers better or create a working answer?
Try:
print {1: 'one', 2: 'two'}.get(a, 'none')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With