Home > Software design >  Is it possible to use elif in a one-line if statement?
Is it possible to use elif in a one-line if statement?

Time:03-05

Example code:

print(one) if number == "1" elif print(two) if number == "2" else print("Please select 1 or 2")

at the elif it expected "else". Is there a workaround for this?

CodePudding user response:

Certainly, you should provide a language; however in python, it works like this:

print('one' if number == 1 else 'two' if number == 2 else 'Please select 1 or 2')

CodePudding user response:

In python:

number = 1

print('one' if number == 1 else 'two' if number == 2 else 'Please select 1 or 2')
  • Related