05 - Selection - IF, ELIF, ELSE

 1. The if elif else statement is used in Python for ...

  creating a new function

  creating loops and noops

  decision making, conditional logic and controlling the flow of a program

  ending a program

 2. In the following code that uses selection (if statements) what is the output?
num = 3

# If you are coding this for yourself, try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
    print("Negative number")					



  Positive or Zero

  Negative number

 3. In the example below, which list will be printed for the user if the user input for 'budget' is 4000.
def cars():

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




  Both expensive_cars and cheap_cars will be printed

  Nothing will be printed

 4. Syntax matters in python. What is missing from the if and else statements in line 4 and line 6?
def login():
     username=input("Enter username:")
     password=input("Enter password:")
     if username=="hello" and password=="open123"
          print("Access Granted")
          print("Access Denied")

  A colon: ":" should always follow the if or else statement

  A full stop "." is missing

  A semi colon ";" should always follow the if or else statement

  Triple speech marks """ are missing

 5. The code below checks an input email for an @. Why will the code fail to run?
def email_check():
     email=input("We would like your email address for our records, Please enter it:")
     if "@" in email:
     print("That is a valid email, Thank you")
     print("That doesn't appear to be a valid e-mail.")


  Because line 4 does not make sense

  Because there should be a bracket around the variable email

  Because emails do not have @

  Because line 5 and line 7 should be indented

 6. Here is some code for a typical menu system. What would happen if the user entered '1'?
import sys
def main():
  ==Main Menu==
  1. Register
  2. Login
  3. Quit
  choice=int(input("Enter choice:"))
  if choice==1:
  elif choice==2:
  elif choice==3:

  The user would be prevented from entering 1

  The user would be asked to re-enter the number

  The user would be taken to the "login" subroutine

  The user would be taken to the "register" subroutine

 7. In the below code, can you spot and point out the mistake on lines 2 and 3?
def main():
	if x=0:
		print("x is greater than 2")


  It should be x=0 on line 2 and if x==0: on line 3

  0 is not really a number so cannot be used in Python

  The variable x can never ever be equal to zero

  There are no mistakes

 8. Entering 3333 in the program below would result in the output: "Access Granted, Welcome AGENT"
def secretcode2():

     print("======SECURITY PASS REQUIRED====")
     code=input("Enter your four digit secret code:")
     if code=="7777" or code=="3333":
          print("Access Granted, Welcome AGENT")
          print("SECURITY ALERT!!!!! IMPOSTOR")




 9. In the following code the 'and' is used to construct an if statement with multiple conditions. When you run the program, what will the output be?
if x>0 and y==3 and x+y < 10:
  print("All the conditions have been met")
  print("Not All the given conditions were met")

  Not All the given conditions were met

  Hi there, x >0 and y==3 and x+y < 10:

  x+y is not less than 10

  All the conditions have been met

 10. What does an IF statement and an ELSE statement represent?

  An if statement represents a loop. An else statement represents another route through the program

  An If statement represents a question. An else statement represents the path to follow when the answer to the question is False

  An if statement represents confusion. An else statement represents hateful deviation

  An if statement represents cautiousness. An else statement represents risky behaviour