if elif else statements (class slides)

CSc 110 if elif else statements

Announcements

  • Check grades on D2L and your submission on gradescope
  • Midterm 1 on October 2 (in class, bring PHOTO ID)

if elif else

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

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

Rewrite the code

Rewrite the code below to use elif and else

def polarity(x):
  result = ""
  if x > 0:
    result = "positive"
  if x < 0:
    result = "negative"
  if x == 0:
    result = "zero"
  return result

Quiz 04

current time

You have 10 minutes to complete the quiz

  • No need for comments
  • No need for a main()
  • No need to print test cases
  • Just write your function and what’s inside the function

Built-in functions you can use: round(), input(), float(), str(), int() — you don’t have to use all of these

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

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