if elif else statements (slides)

CSc 110 if elif else statements

if elif else

While the if condition is required, the elif and else statements are not.

elif is saying ‘if the previous conditions were not true, then try this condition’. It forces the cases to be mutually exclusive.

if conditionA:
    statements
elif conditionB:
    statements
elif conditionC:
    statements
else:
    statements

Compare the functions

def func_one(a):
  message = ""
  if a >= 18:
    message += 'You may apply to join the military.'
  if a >= 35:  # independent from a >= 18
    message += ' You may run for president.'
  return message

def func_two(a):
  message = ""
  if a >= 18:
    message += 'You may apply to join the military.'
  elif a >= 35:  # only runs when a >= 18 is False
    message += ' You may run for president.'
  return message

def main():
  print(func_one(40))
  print(func_two(40))
  
main()
You may apply to join the military. You may run for president.
You may apply to join the military.

Write a function

Write a function that does the following:

  1. Its name is max_of_two
  2. It takes two numeric arguments
  3. It returns the highest value

Test cases

print( max_of_two(-1, 3) ) # 3
print( max_of_two(-1, -3) ) # -1
print( max_of_two(5, 5) ) # 5

Write a function – solution

def max_of_two(x, y):
  if x >= y:
    return x
  else:
    return y

def main():
  print( max_of_two(-1, 3) ) # 3
  print( max_of_two(-1, -3) ) # -1
  print( max_of_two(5, 5) ) # 5

main()
3
-1
5

Write a function

Write a function that does the following:

  1. Its name is max_of_three
  2. It takes three numeric arguments
  3. It returns the highest value

Name your file max_of_three.py and submit to Gradescope.

Test cases:

print( max_of_three(-1, 3, 3) ) # 3
print( max_of_three(-1, -3, 0) ) # 0
print( max_of_three(5, 5, 10) ) # 10

Write a function – solution 1

def max_of_three(x, y, z):
  if x >= y and x >= z:
    return x
  elif y >= x and y >= z:
    return y
  else:
    return z

def main():
  print( max_of_three(-1, 3, 3) ) # 3
  print( max_of_three(-1, -3, 0) ) # 0
  print( max_of_three(5, 5, 10) ) # 10

main()
3
0
10

Write a function – solution 2

def max_of_three(x, y, z):
  max_value = x
  
  if y >= max_value:
    max_value = y
    
  if z >= max_value:
    max_value = z
    
  return max_value

def main():
  print( max_of_three(-1, 3, 3) ) # 3
  print( max_of_three(-1, -3, 0) ) # 0
  print( max_of_three(5, 5, 10) ) # 10

main()
3
0
10

Write a function – solution 3

def max_of_two(x, y):
  if x >= y:
    return x
  else:
    return y

def max_of_three(x, y, z):
  max_x_y = max_of_two(x, y)
  return max_of_two(max_x_y, z)

def main():
  print( max_of_three(-1, 3, 3) ) # 3
  print( max_of_three(-1, -3, 0) ) # 0
  print( max_of_three(5, 5, 10) ) # 10

main()
3
0
10

Write a function

Write a function that does the following:

  1. Its name is average_of_highest
  2. It has three numeric parameters: x, y and z
  3. It returns the average of the two highest of the three arguments
  4. Test cases:
    1. arguments 1, 3, 4 should return 3.5

    2. arguments 6, 4, 2 should return 5.0

    3. arguments 4, 2, 1 should return 3.0

Write a function - solution

def average_of_highest(x, y, z):
  if x >= z and y >= z:
    return (x + y) / 2
  elif y >= x and z >= x:
    return (y + z) / 2
  else:
    return (x + z) / 2
  
def main():
  print( average_of_highest(1, 3, 5) ) # should print 4.0
  print( average_of_highest(6, 4, 2) ) # should print 5.0
  print( average_of_highest(4, 2, 1) ) # should print 3.0
  print( average_of_highest(2, 2, 1) ) # should print 2.0
  print( average_of_highest(2, 1, 2) ) # should print 2.0
  print( average_of_highest(1, 2, 1) ) # should print 1.5
  
main()
4.0
5.0
3.0
2.0
2.0
1.5