If, Elif and Else, the conditional sentences

--Originally published at Hector Martinez Alcantara

We’re going to see now how to make a conditional sentence.

A conditional sentence serves us to tell the program to do an action only if a condition is accomplished. to do it we need to put the word if and then the condition like you can see under this paragraph.

Example of if:

option=int(input('type a number one'))
if option==1:
   print('You have typed one! :)')
   print('Well done!')
print('And its done')

Above you can see that the value that’s assigned in the variable option have a prefix, that assigns the type of variable that the input function will return.

The statement of condition is if the variable option is exactly equal to the number one. If it’s true the program will do the next statements with tab and continue with the code.

If not, it will not do nothing with the lines with tab, but then continue the code.

Example of else:

option=int(input('type a number one'))
if option==1:
 print('You have typed one! :)')
else:
 print('You have not typed one... :(')

An else statement serves to do actions if the above condition is not accomplished.

In the example above, if you don’t type a number one, the sentences under the else statement will be executed.

Elif Example:

print('Lets play a game')
print('You only have three options')
print(' a)Save your wife, but loose a leg')
print(' b)Save your dog, but loose an arm')
print(' c)Save your son, but loose the eyes')
Option= input('Choose an option, if your option is not one of the above mentioned you will die\n')
if Option=='a':
 print('\nLoose your leg')
elif Option=='b':
 print('\nLoose your arm')
elif Option=='c':
 print('\nLoose your eyes')
else:
 print('\nYou have chosen to die, it is ok, you will die but all your loved ones will be ok')

Finally the reserved word elif serves to have diferent

if an statement isn’t true, then checks the next statement, and the next, and the next, and if the program don’t find the correct condition, executes the else statement.

Thanks for reading.

Special thanks to:http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/ifstatements.html