Preview

07 - Selection - If and Case Switch

 1. "Selection" is the term we use to refer to the process of selection or testing a condition. If a condition is met, then *do* something. Analyse the pseudo-code below and see if you can fill in the blanks.
uploads/if_else_switch_case.png

  loop/if /switch/if

  while loop / selection

  if/loop / if/else

  if/else / switch/case

 2. What would the output be if the score was changed to 21? Play around (if there is time) with the if statements to create your own example.

  PASS

  IF

  ERROR

  FAIL

 3. The following example uses 'elif' that allows for more conditional tests. The elif on line 4 has ____ conditional tests. Greater than or equal to 1 AND less than ____. The output here is ______________________________
members_in_pool= 2
if members_in_pool == 0:
    print('The swimming pool is empty')
elif members_in_pool >= 1 and members_in_pool <10:
    print('There is room in the pool')
else:
    print('No Room in the pool')

  Two / 0 / No Room in the pool

  One / 10 / There is room in the pool

  10 / Two / No Room in the pool

  Two / 10 / There is room in the pool

 4. Sometimes there are multiple conditions that could be True and in this case you should use an use the _____ operator to do a membership test in a sequence of accepted elements in a list. The output if the user's response is: "Yep" would be _____
valid_response = ["Yes", "yes", "y"]
response=input("Do you agree?:")
if response in valid_response:
  print("So glad you agree with us!")
else:
  print("Goodbye")

  "if" / Goodbye

  elif / Goodbye

  else / So glad you agree with us!

  "in" / Goodbye

 5. In the following clip from the film "Terminator", where is selection taking place?

  The terminator (a robot) searching for a specific person. If found, output "MATCH"!

  The terminator is using variables and not selection in this case

  The terminator is repeating several actions as he walks through the room.

  There is no evidence of selection in this video at all

 6. Analyse the diagram (pseudocode and flow chart for selection) below and see if you can fill in the blanks.
uploads/ifelse_flowchart.png

  1. x 2. FAIL 3. PASS 4. END IF

  1. x 2. PASS 3. FAIL 4. END IF

  1. 50 2. FAIL 3. PASS 4. CLOSE

  1. x 2. PASS 3. PASS 4. CLOSE LOOP

 7. What statement best describes what is happening in this code?
score = input("Enter score: ")
score = int(score)
if score >= 80:
    grade = 'A'
else:
    if score >= 70:
        grade = 'B'
    else:
        if score >= 55:
            grade = 'C'
        else:
            if score >= 50:
                grade = 'Pass'
            else:
                 grade = 'Fail'
print ("\n\nGrade is: " + grade)  

  Nested if statements have been used to give a score based on a user's grade

  Extra if statements have been used to give a grade depending on constant scores

  Nested if statements have been used to give a grade based on user's score

  Extra if statements used to predict a score based on a grade

 8. What would be output if the user entered: 70,000 for Salary, and '3' for age?
salary = float(input("Enter your annual salary, (e.g. 50000): "))
age=int(input("Enter age:"))
if (salary > 50000) or (age > 21):
    print ("Join our club: because you earn more than $50000 OR you are over 21")
else:
    print ("Sorry. Access Denied")
    

  Join our club: because you earn more than $50000 OR you are over 21

  repeat: "Enter Age"

  Sorry. Access Denied

  Error

 9. Analyse the two (imperfect) pseudocode examples below. The first one uses the AND________________ and the second uses ________________. They basically achieve the same result! Both conditions must be true (correct) for the "Pass" to be output
#Example 1

IF fname = Joe AND sname = Doe THEN
OUTPUT Pass
ELSE Fail
END IF


#Example 2

IF fname = Joe THEN 
    IF sname = Doe THEN
			OUTPUT Pass
	END IF
OUTPUT Fail
END IF

  if statements / else statements

  nested selection statements / boolean operators

  boolean operators / nested boolean statements

  boolean operator / nested selection statements

 10. Analyse the pseudo code for a nested IF statement below. Which statement is accurate?
IF condition1 THEN
	 IF condition2 THEN
…
	END IF
END IF

  If condition2 is FALSE then condition1 is evaluted

  Condition2 is evaluated only if condition1 is True

  Both conditions are only evaluted if condition2 is True

  Condition1 is evaluated only if condition2 is True