Preview

07 - Decisions Control Flow IF Else

 1. The 'SELECTION' statements that are available in programming are IF -THEN - ELSE and SWITCH-CASE.
Note: Python does not support switch-case but a language like VB.Net does if you with to try them out

  FALSE

  TRUE

 2. Why is selection or an IF ELSE statement used in a program?
Note: The following video from www.teachyourselfpython.com outlines selection in Python. 

  To control the flow of a program (i.e decisions lead to different paths in a program)

  All of the above

  To help address a decision or a question

  To cause the program to follow a certain step and ignore the others

 3. Run the following code, if possible, or trace the logic, to see what it does. How are the equal signs on line 4 and 5 different?
def secretcode1():
     #Change the secret code to 4444. Test the result to see if it still works.
     print("======SECURITY PASS REQUIRED====")
     code=input("Enter your four digit secret code:")
     if code=="7777":
          print("Access Granted, Welcome AGENT")
     elif code=="7776": #note the use of ELIF which is basically like ELSE IF
       print("Close, but not quite")
     else:
          print("SECURITY ALERT!!!!! IMPOSTOR")
           
secretcode1()

  None of the above

  They are not different - they are in fact performing the same function

  On line 4 the equal sign is an assignment operator and on line 5 it is a double equal sign checking equivalence to

  On line 4 the equal sign is checking equivalence and on line 5 the double equal sign is an assignment operator

 4. What is ELIF likely to stand for or mean?
Note: Feel free to watch this coding demo on IF - ELSE - ELIF

  Else IF …

  Event Follow - IF

  None of the above

  Event Like Indent Follow

 5. Analyse the code carefully. If the user enters '3333' , what will happen?
def secretcode2():
     #Here we are using an "or" to give more options. Add a few more secret codes,
     #assuming there are a few agents. e.g. 3322, 0007 and 0002. Test to see they work!
     print("======SECURITY PASS REQUIRED====")
     code=input("Enter your four digit secret code:")
     if code=="7777" or code=="3333":
          print("Access Granted, Welcome AGENT")
     else:
          print("SECURITY ALERT!!!!! IMPOSTOR")

secretcode2()

  They will be denied access

  There will be an error, as '3333' is not an integer

  They will be granted access

  There will be a security alert as well as a critical run time error as only 7777 would be accepted

 6. Run the program and analyse it. If the user enters 2000 it should present the expensive cars, but it doesn’t. How can we fix this?
def cars():
     #Change the budget to over 5000 in order to display the expensive cars
     #Add a few more cars to each list
     expensive_cars=["Mercedes","Audi","BMW"]
     cheap_cars=["Skoda","Maruthi","Nano"]

     budget=int(input("How much can you spend on a car?"))
     if budget>2000:
          print("Here are some cars you could consider",expensive_cars)
     else:
          print("You may want to check out these brands:",cheap_cars)
     cars()

cars()

  This cannot be done as the only available operators to use with an if statement are > and <

  None of the above

  Change line 8 to:if budget=>2000:

  Change line 8 to: if budget>=2000:

 7. Analyse the two code blocks below. Which of the following statements is likely to be true?
Both these programs do the same thing. 

#Program 1
===========
def letterGrade(score):
    if score >= 90:
        letter = 'A'
    else:   # grade must be B, C, D or F
        if score >= 80:
            letter = 'B'
        else:  # grade must be C, D or F
            if score >= 70:
                letter = 'C'
            else:    # grade must D or F
                if score >= 60:
                    letter = 'D'
                else:
                    letter = 'F'
    return letter


Program #2
===========
def letterGrade(score):
    if score >= 90:
        letter = 'A'
    elif score >= 80:
        letter = 'B'
    elif score >= 70:
        letter = 'C'
    elif score >= 60:
        letter = 'D'
    else:
        letter = 'F'
    return letter

  All of the above statements are true

  The repeatedly increasing indentation with an if statement as the else block in Program 1 can be confusing and distracting.

  Program 2 also is more efficient as it combines each else and if block into an elif block:

  Program 2 is a preferred alternative in this situation, as it avoids all indentation

 8. There are two errors in the following code - can you spot what they are?
def eye_colour_check():
  eyecolour=input("Enter eye colour:")
  if eyecolour != "Black"
    print("Sorry, we reject you on the basis of your eye colour")
    else:
    print("Welcome, superior eye-coloured human")

eye_colour_check()

  None of the above - there are no errors. This program will run fine

  Missing colon after Black on line 3 and the 'else' on line 5 is not indented in line with the if

  Missing indent on line 3 and a missing colon on line 5 which should be in line with the 'else'

  Missing brackets around the 'Black' on line 3 and an additional colon after the else on line 5 which is not needed

 9. What are conditions? They check to see whether something is true or false. 2 < 5 will evaluate to:

  FALSE

  TRUE

 10. The expression 3>7 will evaluate to:

  FALSE

  TRUE

 11. If, elif and else are ___________ in Python

  functions

  command lines

  keywords

  programs

 12. Typical conditions are shown in the list below. What is the !=
xy, x<=y, x>=y, x!=y and x==y

  check for errors

  not equal to

  equal to if also equal to something else

  equals to in all scenarios

 13. Analyse the code below. If the user entered '22' what would the output be?
x = int(input("What is the time?"))

if x < 10:
 print "Good morning"

elif x<12: 
 print "Soon time for lunch"

elif x<18: 
  print "Good day"

elif x<22: 
 print "Good evening"

else: 
  print "Good night"

  Good morning

  Good evning

  Soon time for lunch

  Good night

 14. An if ... elif ... elif ... sequence is a substitute for the switch or case statements found in other languages

  TRUE

  FALSE

 15. Generally speaking the 'else' part of an if elif seqeuence (with else at the end) will run if ….

  all others fail

  the if statement is executed to begin with

  all other elif statements are executed

  The else is never executed -it is just decorative

 16. On what line are the errors?
x = int(input("What is the time?"))

if x < 10 ...
 print("Good morning")

elif x<12: 
 print("Soon time for lunch")

  elif x<18: 
  print("Good day")

elif x<22: 
 print("Good evening")

else: 
  print("Good night")

  Line 2 and Line 8

  Line 3 and Line 9

  Line 9 and Line 15

  Line 3 and Line 10

 17. On what line is the error that is stopping this code from running?
def cars():
     #Change the budget to over 5000 in order to display the expensive cars
     #Add a few more cars to each list
     expensive_cars=["Mercedes","Audi","BMW"]
     cheap_cars=["Skoda","Maruthi","Nano"]

     budget=int(input("How much can you spend on a car?"))
  if budget=>2000:
          print("Here are some cars you could consider",expensive_cars)
     else:
          print("You may want to check out these brands:",cheap_cars)
     cars()

cars()

  Line 7 - If statement error

  Line 10 - there should be no colon after the else:

  Line 8 - indentation error

  There is no error

 18. There are two errors in this code - can you spot them?
def secretcode1():
     code=input("Enter secret code:")
     if code=7777:
       print("ACCESS GRANTED")
          else:
      print("DENIED")
           
secretcode1()

  There are no errors

  Line 1 and Line 2

  Line 3 and Line 5

  Line 2 and Line 5

 19. You want to accept any number over 7, including 7 as a secret code. What should line 3 be changed to?
def secretcode1():
     code=input("Enter secret code:")
     if code==7777:
       print("ACCESS GRANTED")
     else:
      print("DENIED")
           
secretcode1()

  if int(code)>=7:

  if code > 7

  if code =>7:

  if code>=7:

 20. How could a game like minecraft use selection? (if statements)

  If statements are typically not used in advanced programming

  If statements only exist in Python and are not used in other languages such as Javascript or VB.Net

  If there is a rock in front of a character, turn left or right (do not collide with rock)

  Most games never use if statements, they only use loops.