Preview

05 - Selection - IF, ELIF, ELSE

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

  ending a program

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

  creating a new function

  creating loops and noops

 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")
else:
    print("Negative number")					

  else:

  Error

  Negative number

  Positive or Zero

 3. In the example below, which list will be printed for the user if the user input for 'budget' is 4000.
def cars():
   
     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()

  Both expensive_cars and cheap_cars will be printed

  cheap_cars

  Nothing will be printed

  expensive_cars

 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")
     else
          print("Access Denied")
   
     
login()

  Triple speech marks """ are missing

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

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

  A full stop "." is 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")
     else:
     print("That doesn't appear to be a valid e-mail.")

email_check()

  Because line 4 does not make sense

  Because line 5 and line 7 should be indented

  Because emails do not have @

  Because there should be a bracket around the variable email

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

  The user would be taken to the "login" subroutine

  The user would be asked to re-enter the number

  The user would be prevented from entering 1

  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():
	x==0
	if x=0:
		print("x is greater than 2")

main()

  The variable x can never ever be equal to zero

  There are no mistakes

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

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

 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")
     else:
          print("SECURITY ALERT!!!!! IMPOSTOR")

secretcode2()

  False

  True

 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?
x=2
y=3
if x>0 and y==3 and x+y < 10:
  print("All the conditions have been met")
else:
  print("Not All the given conditions were met")

  x+y is not less than 10

  All the conditions have been met

  Not All the given conditions were met

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

 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 confusion. An else statement represents hateful deviation

  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 cautiousness. An else statement represents risky behaviour