Python | Conditional Statements | Elif Statement (else-if Statement)

The elif statement is used to check multiple conditions. When the condition is met, the rest is ignored.

The syntax for elif statement is as follows.

if [condition 1 is true]:
	do something
elif [condition 2 is true]:
	do something
elif [condition 3 is true]:
	do something
	do something else if all conditions are false

As an example, we modify the previous code into something like is shown below.

option_one = input('Type a number from 1 to 3 here: ')
if option_one == 1:
	print('number 1 is your option')
elif option_one == 2:
	print('number 2 is your option')
elif option_one == 3:
	print('number 3 is your option')
	print('that is not a good option')

The same as before, when the code is run an input is asking the user to type in a number. If the user types in a number between 1 and 3 the message will be printed on the screen accordingly. If the user types a number other than 1, 2, or, 3 then the message from else statement will be displayed.

Elif Statement (else-if Statement)

Leave a Reply