How do I create an if statement in Python?

1) First lets look at my code. Right now I have listed members of a house. Each member of the house has a name and an age, these are variables. For example sister is a variable and has an age of 12. Finally I am asking you to put your age in by asking "How old are you?"

2) Now lets put in the first branch of our "if statement". Take a look, I am actually using math symbols. I am saying: If the number you write as your age is less than the age of the sister, dad, mom, brother variable the program will print out a line that says "You are the youngest".

3) Now lets put another branch in. Notice that I am using "elif" on the 2nd line. This is letting us give another option, always use elif on the second line if you need more options. Notice the 2nd line is NOT indented below the first branch, it gets its own line.

*The second branch is telling the program to say that you are the oldest, look at the greater than signs in the branch.

4) What happens if you are equal to someone elses age? We can actually create that option. Just use another "elif"!

*Notice I am using "or" instead of "and" in this branch. This is because we cannot be the same age as everyone in the house.

5) So far our program works if: you are the oldest, you are the youngest, you are the same age as someone. The only option left is if you are older than the others but not the oldest. Lets put that in using "else". Else is the final option you use to cover everything else that was not in the branches above.

*Else is easy, no need to do any more math!

© 2020 Farragut Technology Program