An If Statement works the same way it does in Scratch.
It is used to make choices.
It makes a choice by comparing a variable to a value.
The If Statement structure follows this pattern:
if {conditions}:
{run this code}
elif {conditions}:
{run this code}
elif {conditions}:
{run this code}
else:
{run this code}
The elif statement is for any other cases after the first one. Think of it like branches on a decision tree:
Each elif is a condition added to the first one with the last else statement representing "if everything else doesn't work, do this thing"
For instance, lets say I give you $5 to go buy me the daily special at a store and you're making a program for what the person will say at the cash. I want chips the most, but if not get me a chocolate bar or buy nothing.
if whatisonsale == "chips":
print "I buy the chips"
elif whatisonsale == "chocolate bar":
print "I buy the chocolate bar"
else:
print "I buy nothing"
Now, some of you will probably think you can skip the elif and just write a bunch of if statements. THIS IS BAD PROGRAMMING!!!
This is what it would look like:
if whatisonsale == "chips":
print "I buy the chips"
if whatisonsale == "chocolate bar":
print "I buy the chocolate bar"
The main thing wrong with this is you've broken up the choices so they're not related to the branch. Putting other related choices as elif statements helps other people see the relationship of the choices. The person also does not say "I buy nothing".
Condition Options
These are all the comparisons you can make
== equal to (TWO equal signs, not one)
!= not equal to
< less than
<= less that or equal to
> greater than
>=greater than or equal to
Type this program in. Remember that you can still save these programs for your archive and if you do, name them appropriately (like If-Example 1), and you don't have to submit them. The program tests to see if a number is higher, lower or equal than a 2nd number.
The spacing in line 7, 9 and 11 is one TAB space. This TAB indent is SUPER important. It is how Python organizes what is within the if statements or branches! If your indents are incorrect, you will be flagged for an error.
Analysis Questions: (THINK about these so you can improve your thought process. Answers are below).
Answers to Analysis Questions:
Embedded If Statements
These are if statements INSIDE of if statements. Like the Inception movie but for if statements.
Try this example below by first reading the code and predicting what it will do. Then type it out and try it.
This code tells you if your number is greater than 5, less than 5, the number 5 or greater than 10.
It uses an embedded if statement plus elif and else to show you an application of that kind of code.
If Statement Assignment
Create a program that allows you to pick items from a restaurant menu using numbers only.
It will offer you 3 options of restaurants, but ONLY PROGRAM THE FIRST ONE.
ONLY PROGRAM THE FIRST ONE (I'm saying this twice because someone is not going to pay attention and do the whole thing).
After picking from a list of 3 main meals, you will get 3 options of picking a side.
After picking a side dish, it will tell you what your choices were.
The output should look as close to the following picture as possible.
Put your name in comments at the top and bottom of your program and submit the link below.