Python Style
use the left and right arrow keys to navigate
by Paula Bannerman dcartist.com
One thing to remember
It's not that bad
It just seems bad
You actually do conditionals (if/else) in your life every day.
If Else statements are like giving choices (based on certain information) to someone who hasn't been exposed to any of it before. It's just it's a computer not a human.
Let's just say you have a light switch set
to off
light = "off"
You tell someone to turn the light on it if the light is off it
would look like this:
light = "off"
if light == "off":
light = "on"
Now you can tell them if it's off turn it on
or if it's on turn it off:
light = "on"
if light == "off":
light = "on"
else:
light = "off"
Let's look at the structure
if "this condition is true":
"do something"
elif "this other condition is true":
"do something else "
else:
"do this since nothing above is true"
if "this condition is true":
"do something"
elif "this other condition is true":
"do something else "
else:
"do this since nothing above is true"
you start with an if and a condition you want to check
if "this condition is true":
"do something"
elif "this other condition is true":
"do something else "
else:
"do this since nothing above is true"
if it's true then do something
if "this condition is true":
"do something"
elif "this other condition is true":
"do something else "
else:
"do this since nothing above is true"
else if something else is true do something else
else if is written as elif
if "this condition is true":
"do something"
elif "this other condition is true":
"do something else "
else:
"do this since nothing above is true"
When nothing in the conditional came out to be true you can use else as a catch all.
When you need an if else inside another if statement, then you will indent that one on the inside. Like adding a container inside another container.
if ("this condition is true") :
do something
elif ("this other condition is true"):
if ("this condition is true") :
do something
elif ("this other condition is true"):
"do something else "
else:
"do this since nothing above is true"
else:
"do this since nothing above is true"
broke = False
outside_temp = 75
if broke == True:
print"FREE office coffee"
elif outside_temp <= 60:
print("Hot Coffee from Starbucks")
else:
print("Starbucks Ice Coffee")
First we find something to compare, which in this case is being broke and the weather
broke = False
outside_temp = 75
if broke == True:
print"FREE office coffee"
elif outside_temp <= 60:
print("Hot Coffee from Starbucks")
else:
print("Starbucks Ice Coffee")
If you are broke, then you are drinking FREE office coffee
broke = False
outside_temp = 75
if broke == True:
print"FREE office coffee"
elif outside_temp <= 60:
print("Hot Coffee from Starbucks")
else:
print("Starbucks Ice Coffee")
else if (elif) it's cold outside then you're drinking Hot Coffee from Starbucks
broke = False
outside_temp = 75
if broke == True:
print"FREE office coffee"
elif outside_temp <= 60:
print("Hot Coffee from Starbucks")
else:
print("Starbucks Ice Coffee")
else then you're drinking Ice Coffee from Starbucks
Think in simple terms
If something is true then do this...
Else If (elif) something other than the first statement is true then do this...
Else just do this and call it a day...
Watch your indentation
for else if it's called elif
just remember you can do tons with if/else
keep practicing using them