elifelses

There is the code I made to put at work the conditionals. The program basicaly tells you if the number you typed is a float, an integer, a negative integer, or a negative float.

So first you ask for the number

x = input ( “Number” )

easy, then here comes the conditional.

if

if is called conditional, python will run everything “inside” the if, IF the condition inside the parenthesis is true

So first I will ask python if the number the user gave me is a negative float; if it has a “-” and a “.”

to do it, I store the point in a token name floatt, and the minus in a token named minus

minus = “-”                floatt = “.”

ifofelif

So    

if ( (floatt in x) and (minus in x)):
      print (“It is a negative float”)

What you are typing is: IF the point storaged in FLOATT is inside the number stored in X, AND the minus storaged in MINUS is inside the number stored in X, then it is a negative float, and the program will say it to you.

elif

elifs

We already have the condition to a negatiuve float, but we don’t have he conditions of float or negative.

But we can’t put another if because the program will run the first condition, then the second one, and so on, so a negative float is a negative and a float, that’s why elif is so useful.

You can ask, if the IF conditions didn’t met, another condition with elif.

So

elif (floatt in x):
       print  ( “It is a float” )

elif (minus in x):
       print ( “It is a negative number”)

You are asking only when the first coindition don’t met if FLOATT  is inside of X, if not, then you ask if MINUS is inside X.

else

else

Finally, if none of the previus conditions were met, ELSE finally gets into scene, then it is a integer,

So

else:
      print ( “It is an integer” )

An that is pretty much all! You know how to use if, elif, else conditionals!

CC BY 4.0 The basics of programing: if, elif, else Masteries 15, 16, 17 by charliegdrummer is licensed under a Creative Commons Attribution 4.0 International License.