Preview

01 - Selection (IF,ELSE)

 1. In programming, selection is implemented using WHILE statements
Note: the following powerpoint goes through some of the basics of selection in python. 

  FALSE

  TRUE

 2. What is happening on line 3 of this code?
nl=[]
for x in range(1500, 2701):
    if (x%7==0) and (x%5==0):
        nl.append(str(x))
print (','.join(nl))

  checking to see if any of the numbers between 1500 to 2700 are divisible by 7 and are a multiple of 5.

  checking to see if the number x (either 1500 or 2701) can be divided by either 7 or 5

  checking to see if any of the numbers between 1500 and 2701 when divided by 7 or 5 produce 0.

  checking to see if 1500 or 2701 are divisible by 7 or is a multiple of 5

 3. Can you spot the error in this code? If the user enters 50, it doesn't display the desired message.
age = int(input("How old are you?"))
if age >= 70:
	print("You are aged to perfection!")
elif age = 50:
	print("Wow, you are half a century old!")
else:
	print("You are a spring chicken!")

  Change line 2 to: if age > 50 or 70:

  Change line 4 to: else age = 50:

  Change line 4 to: else age="50":

  Change line 4 to: elif age == 50:

 4. When using an ‘if...else if’ statement, the program will ____________________________. It is therefore very important to get the ‘if’ and ‘else’ conditions in the right order to make the program as efficient as possible
age = int(input("How old are you?"))
if age >= 70:
	print("You are aged to perfection!")
elif age == 50:
	print("Wow, you are half a century old!")
elif age >= 18:
	print("You are an adult.")
else:
	print("You are a spring chicken!")

  All of the above answers are correct

  continue to check even if it finds a positive answer

  return back to the 'if' statement when the answer has been found

   stop checking as soon as it finds a positive answer

 5. In the following python code, when would the 'else' part of the code on line 11 and 12 be executed?
temp = input("Input the  temperature you like to convert? (e.g., 45F, 102C etc.) : ")
degree = int(temp[:-1])
i_convention = temp[-1]

if i_convention.upper() == "C":
  result = int(round((9 * degree) / 5 + 32))
  o_convention = "Fahrenheit"
elif i_convention.upper() == "F":
  result = int(round((degree - 32) * 5 / 9))
  o_convention = "Celsius"
else:
  print("Input proper convention.")
  quit()
print("The temperature in", o_convention, "is", result, "degrees.")

  if the input is "19C"

  if the input is "22D"

  if the input is "22F"

  if the input is "100C"

 6. In reference to the following code,if the input is 3, what would the ouput be?
x = int(input("Enter a number:"))
if x < 0:
    x = -x
print(x)

  3

  30

  6

  -3

 7. What inputs would be necessary in order for this program to output: "YES"
a = int(input("Enter a number:"))
b = int(input("Enter another number:"))
if a % 10 == 0 or b % 10 == 0:
    print('YES')
else:
    print('NO')

  1 and 01

  100 and 4

  7 and 4

  1 and 2

 8. In the following code, what needs to go in line 1 in order for the rest of the if statement to work?
#What needs to go here?
if food == 'twix':
    print('Ummmm, my favorite!')
    print('There is just no chocolate like twix...')
    print(100 * (food + '! '))

  food=food

  food = "snickers":

  twix = food

  food = 'twix'

 9. In what situation would the 'else' part of this code be executed?
choice=input("Enter choice:")
if choice == 'a':
    print("You chose 'a'.")
elif choice == 'b':
    print("You chose 'b'.")
elif choice == 'c':
    print("You chose 'c'.")
else:
    print("Invalid choice.")

  if the user enters anything other than 'a', 'b' or 'c'

  If the user enters '2'

  All of the listed options are valid answers

  if the user enters

 10. What is the output of the following, and why?
if True:
  print("This")

  Error, because there is no if statement condition defined in line 1

  This' because line 1 is always True

  This' is printed because line 1 is False in this case

  None of the listed options are correct

 11. The following code appears to run, but on entering '100' doesn't produce the desired output. Why?
score=input("Enter your score percentage:")
if score==100:
  print("Congratulations - a perfect score")
  

  None of the listed options are correct

  Because on line 1, the score has not been converted to an integer, and it cannot check for equivalence on line 2

  Because there are two equal signs on line 2, and there should be only one

  Because there is an additional colon

 12. Why will the following code not run?
name=input("Enter your name:")
if name=="Joe"
 print("Welcome ",name)
  

  None of the listed options are correct

  Because you can only enter 'Joe'

  Because the indent on line 3 is incorrect

  Because there is a missing colon after line 2

 13. What is wrong with the following code (why will the if statement not execute)
x=1
if x==1:
print(x)
else:
  print("x is not 1")
  

  There is an additional colon after else, which is not needed

  Everything inside an if statement should be indented and line 3 is not indented

  Nothing is wrong - it will work fine

  There is a double equal on line 2 instead of a single equal sign

 14. Can you spot the error in the if statement?
password="open123"
if password="open123":
  print("Welcome")
else:
  print("Rejected")

  No quotation marks necessary on line 2 around the "open123"

  Double equal sign (to check for equivalence) needed on line 2

  There is no error

  No colon needed at the end of line 2

 15. From python's documentation: The keyword ‘elif’ is short for ‘else if’, and is useful to ………..

  avoid excessive indentation

  avoid multiple loops

  avoid the use of large variables

  avoid the use of boolean flags