Conditionals and Control Flow

I Introduction to Control Flow

i. Go with the Flow 1/15

Save and Submit

II Comparators

i. Compare Closely! 2/15

# Assign True or False as appropriate on the lines below!

# Set this to True if 17 < 328 or to False if it is not.
bool_one = True # We did this one for you!

# Set this to True if 100 == (2 * 50) or to False otherwise.
bool_two = True

# Set this to True if 19 <= 19 or to False if it is not.
bool_three = True

# Set this to True if -22 >= -18 or to False if it is not.
bool_four = False

# Set this to True if 99 != (98 + 1) or to False otherwise.
bool_five = False

ii. Compare…Closelier 3/15

# (20 – 10) > 15
bool_one = False # We did this one for you!

# (10 + 17) == 3**16
# Remember that ** can be read as ‘to the power of’. 3**16 is about 43 million.
bool_two = False

# 1**2 <= -1
bool_three = False

# 40 * 4 >= -4
bool_four = True

# 100 != 10**2
bool_five = False

iii. How the Tables have Turned 4/15

# Make me true!
bool_one = 3 < 5 # We already did this one for you!

# Make me false!
bool_two = 4 != 2**2

# Make me true!
bool_three = 5 <= 2**3

# Make me false!
bool_four = 6 == 2*5

# Make me true!
bool_five = 8<= 59.1

III Boolean Operators

i. To be or not to be 5/15

Save and Submit

ii. And 6/15

bool_one = 1 > 3 and 3 < 3

bool_two = -(-(-(-2))) == -2 and 4 >= 16**0.5

bool_three = 19 % 4 != 300 / 10 / 10 and 15 > 21

bool_four = -(1**2) < 2**0 and 10 % 10 <= 20 – 10 * 2

bool_five = 6 < 7 and 8 >= 3

iii. Or 7/15

bool_one = True

bool_two = True

bool_three = False

bool_four = True

bool_five = False

iv. Not 8/15

bool_one = False

bool_two = True

bool_three = True

bool_four = True

bool_five = False

v. This and That (or This, But Not That!) 9/15

bool_one = False

bool_two = True

bool_three = True

bool_four = True

bool_five = False

vi. Mix ‘n’ Match 10/15

# Make me false!
bool_one = (2 <= 2) and “Alpha” == “Bravo” # We did this one for you!

# Make me true!
bool_two = True or not False

# Make me false!
bool_three = False or not False and False

# Make me true!
bool_four = False or not not True and True

# Make me true!
bool_five = not False or not True

IV If, Else and Elif

i. Conditional Statement Syntax 11/15

response = “Y”

answer = “Left”
if answer == “Left”:
print “This is the Verbal Abuse Room, you heap of parrot droppings!”

ii. If you are having.. 12/15

def using_control_once():
if 5<6:
return “Success #1”

def using_control_again():
if 7==7:
return “Success #2”

print using_control_once()
print using_control_again()

iii. Else Problems, I Feel Bad for You, Son… 13/15

answer = “‘Tis but a scratch!”

def black_knight():
if answer == “‘Tis but a scratch!”:
return True
else:
return False
# Make sure this returns False

def french_soldier():
if answer == “Go away, or I shall taunt you a second time!”:
return True
else:
return False
# Make sure this returns False

iv. I Got 99 Problems, But a Switch Ain’t One 14/15

def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
return 0

print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

V Review

i. The Big If 15/15

def the_flying_circus():
if (3 > 2) and (4 > 7):
return “Yes this one is true.”
elif True and not False:
return True
else:
return “None is true.”
print the_flying_circus(4)
print the_flying_circus(5)
print the_flying_circus(6)

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s