Python KPRIDE: IF ELIF ELSE

Last updated 7 months ago
19 questions

For GCSE, you must know the following for programming:

  • Outputs and Inputs
  • Variables
  • Maths Operators
  • Random numbers
  • Strings and Casting
  • Booleans
  • If, elif, else
  • Nested Ifs
  • For loops
  • While loops
  • Nested loops
  • Arrays/Lists
  • 2D Lists
  • File handling
  • Subprograms(functions/procedures)

Warm-up Practice:
https://type.withcode.uk/python/Conditional_logic

Learning Objectives:

  • I know what a condition is
  • I can make multiple IF statements
  • I know that elif and else belong to an IF statement

Keywords

  1. Condition: a value is checked to return True or Flase
  2. Selection: A condition is used to see which lines of code, if any, will run.
  3. Indentent: Spaces given to code
  4. if statement: Runs code that is indendented when a condition is met.
  5. elif statement: Stands for "Else If". Is used as another question if the if statement is not met.
  6. else statement: Runs this code when the if condition is not met.

NOTE:
IF Elif and Else are known as Selection in programming. Selection uses a condition to allow branching code. This links to the Boolean lesson.

We can have many if statements one after another.
elif and else can't exist by itself, it has to have an if statement to belong to. You can also have many elif statements, but only one else.
1

What does the 'if' keyword do in Python?

1

When will 'print('no')' execute?

1

What is the purpose of the 'else' keyword?

1

What does 'a == b' check?

Predict and Run

Look at the code for each question.
What do you think will be the output?
What is the actual output?


Code in each part of if and else is INDENTED to show it belongs
1

What do you think will be the output from the following code:

print ( 10 > 5 )

1

What do you think will be the output from the following code:

if 10 > 5:
print ("Ten is larger than five")

1

What do you think will be the output from the following code:

if 10 < 5:
print ("Ten is larger than five")

1

What do you think will be the output from the following code:

name = "007"
if name == "007":
print("Welcome, Agent 007.")

1

What do you think will be the output from the following code:

myOption = 1
if myOption == 1:
print("Game Start")
elif myOption == 2:
print("Game Load")
elif myOption == 3:
print("Credits")

1

What do you think will be the output from the following code:

myOption = 1
if myOption == 1:
print("Game Start")
elif myOption == 2:
print("Game Load")
elif myOption == 3:
print("Credits")

1

What do you think will be the output from the following code:

myAction = "Hug"
if myAction == "Hug":
print("I hugged her and congratulated on her success.")

if myAction == "Kiss":
print("We shared a kiss and enjoyed the moment...")
else:
print("Where shall we to celebrate?")

1

What do you think will be the output from the following code when the user enters the number 10:

print("Enter a number: ")
odd_even = int(input())
odd_even = odd_even % 2
if odd_even == 1:
print("Your number is odd")
elif odd_even == 0:
print("Your number is even")

1

What do you think will be the output from the following code when the user inputs "Foggy":

print("What shall I do today?")
weather = input("What is the weather like today? ")

if weather == "Sunny":
print("Time to play tennis.")
else weather == "Raining":
print("Read a book")

Investigate

  1. Look at the code on the right.
  2. Run it and see what it does.
  3. Click on the pencil icon at the top to see the code again.

FIRST EXAMPLE:
  1. Run the program and see what it does
  2. Can you add your own elif statement on line12?
Second Example:
  1. Run the program. What does this do? Try entering a number between 1 and 10
  2. Try entering a word
  3. Now try a number below 0 or a number bigger than 10. What happens?
Third Example
  1. Run the program. Try entering xx@email.com
  2. Change the variable for myEmail to make it easier to test.
  3. See the rest of the comments in the code and see what happens

Debug

Click on each link and try dragging and dropping code into the correct order
https://7strikelink7.github.io/NormalPythonParsons/parsons/If.html
https://7strikelink7.github.io/NormalPythonParsons/parsons/Else.html
https://7strikelink7.github.io/NormalPythonParsons/parsons/Elif.html

Extend

Help:
print ( ) is used to output text
input( ) is used to get the user to type something

Variables store values that can change.
Name = Value here

https://www.w3schools.com/python/python_conditions.asp

RECAP
Boolean: a data type that can hold one of two values (true or false)
> Symbol: Greater than
< Symbol: Less than
== Symbol: Compares if both values are the same
!= Symbol: Not equal to
>= Symbol: Greater than or equal to
<= Symbol: Less than or equal to

Selection -

if (condition) :
(indent) program here....

1

Create a program that asks the user to enter a name.
If the name is "Harry Potter", output "The boy who lives!"

1

Adapt the last code so that it checks if the user has entered "Mark".
If it is Mark, output "Are you Mark Hamil! I love Star Wars! Big fan."

if it is anyone else,
output "Hello, [name]"

1

Create a program that asks the user to enter two numbers.
Output which number is larger.

1

Create a program that asks the user to make a new password.
If the length is less than 8, output "That is too short"
if it is longer than 16 characters, output "That is too long!"

1

Create a program that asks the user for a number.
Output whether it is even or odd.

1

Create a calculator program that asks the user for two numbers.

Then ask the user to enter a maths symbol (+-/*)

Perform the calculation with the two numbers and symbol.

e.g.
User enters 5
User enters 6
User enters *
Output 30


CHALLENGE 1:
Cast the user inputs into floats with float() so decimal numbers will work as well.

CHALLENGE 2:
add an option for modulus (%) and Integer division (//)

CHALLENGE 3:
For the maths symbol option, allow the user to enter the symbol ("*") or the word("Multiply") (HINT: Use the Keyword OR)