2018-02-06 HW

posted Feb 6, 2018, 6:22 AM by Konstantinovich Samuel   [ updated Feb 6, 2018, 11:08 AM ]
NEW STUFF

If statements in python work the same way they did in scheme and netlogo:

if a:
   statement1 #only executes when a is true

statement2 #executes all the time because it is not inside of the if




if a:
   statement1 #execute when a is true
else:
   statement2 #execute when a is false

statement3 #executes all the time because it is not inside of the if


Since an if-statement can be put inside another if statement we have to be careful. Different ways of connecting them can produce different effects. (discussed in class)

if a:
   statement1
else:
   if b: 
      statement2

OR...


if a:
   if b: 
      statement1
else:
   if c: 
      statement2

OR...

if a:
   if b: 
      statement1
   else:
      statement2
else:
   if c: 
      statement3

OR...

if a:
   if b: 
      statement1
   else:
      statement2
else:
   if c: 
      statement3
   else:
      statement4
 



You may have noticed that if/else statements that are connected start to indent too much.
if a:
   statement1
else:
   if b: 
      statement2
   else:
      if c:
          statement3
      else:
          statement4

You can re-write these statements by using an elif statement (else+if) as follows.

if a:
   statement1
elif b: 
   statement2
elif c:
   statement3
else:
   statement4


Classwork/Homework
http://codingbat.com/home/konstans@stuy.edu/all
Decisions (numerical)  + More Booleans
8 total problems.
Comments