Python KPRIDE: Nested if
star
star
star
star
star
Last updated 6 months ago
6 questions
Keywords
- Condition: a value is checked to return True or Flase
- Selection: A condition is used to see which lines of code, if any, will run.
- Indentent: Spaces given to code
- if statement: Runs code that is indendented when a condition is met.
- elif statement: Stands for "Else If". Is used as another question if the if statement is not met.
- else statement: Runs this code when the if condition is not met.
- Nested if: An if statement inside another if statement
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.
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 will be the output for the following code if you input "y"?
myAnswer = input("Do you want a twix? y/n")
if myAnswer == "y": print("Here is a twix")else: myAnswer = input("How about a Mars Bar?") if myAnswer == "y": print("Here is a Mars Bar.") else: print("Okay... have a bounty bar then.")
What will be the output for the following code if you input "y"?
myAnswer = input("Do you want a twix? y/n")
if myAnswer == "y":
print("Here is a twix")
else:
myAnswer = input("How about a Mars Bar?")
if myAnswer == "y":
print("Here is a Mars Bar.")
else:
print("Okay... have a bounty bar then.")
1
What will be the output for the following code if you input "n" then "y"?
myAnswer = input("Do you want a twix? y/n")
if myAnswer == "y": print("Here is a twix")else: myAnswer = input("How about a Mars Bar?") if myAnswer == "y": print("Here is a Mars Bar.") else: print("Okay... have a bounty bar then.")
What will be the output for the following code if you input "n" then "y"?
myAnswer = input("Do you want a twix? y/n")
if myAnswer == "y":
print("Here is a twix")
else:
myAnswer = input("How about a Mars Bar?")
if myAnswer == "y":
print("Here is a Mars Bar.")
else:
print("Okay... have a bounty bar then.")
1
What will be the output for the following code if you input "no thanks" then "I don't want a mars bar."?
myAnswer = input("Do you want a twix? y/n")
if myAnswer == "y": print("Here is a twix")else: myAnswer = input("How about a Mars Bar?") if myAnswer == "y": print("Here is a Mars Bar.") else: print("Okay... have a bounty bar then.")
What will be the output for the following code if you input "no thanks" then "I don't want a mars bar."?
myAnswer = input("Do you want a twix? y/n")
if myAnswer == "y":
print("Here is a twix")
else:
myAnswer = input("How about a Mars Bar?")
if myAnswer == "y":
print("Here is a Mars Bar.")
else:
print("Okay... have a bounty bar then.")
Investigate
- Look at the code on the right.
- Run it and see what it does.
- Click on the pencil icon at the top to see the code again.
FIRST EXAMPLE:
- Run the program and see what it does
- Can you add your own elif statement on line12?
Second Example:
- Run the program. What does this do?
- Can you add your own question into this animal guesser?
- A popular version of this program is quite popular called Akinator. Can you add more to make it better?
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", output "Which Harry? What is your last name? "
Then ask for their last name.If they enter "Styles" output "I love your songs!"If they enter "Potter" output "The boy who lives!"Anything else, output "Nice to meet you!"
Create a program that asks the user to enter a name.
If the name is "Harry", output "Which Harry? What is your last name? "
Then ask for their last name.
If they enter "Styles" output "I love your songs!"
If they enter "Potter" output "The boy who lives!"
Anything else, output "Nice to meet you!"
1
Create a program for the start of a videogame - Askthe user to enter a username for the game. Ask them "[Name]. Are you sure?". If they input "yes" Output "Starting game". Else, "Too bad, you are stuck with the name [name]"
Create a program for the start of a videogame -
Askthe user to enter a username for the game.
Ask them "[Name]. Are you sure?".
If they input "yes" Output "Starting game".
Else, "Too bad, you are stuck with the name [name]"
1
Create a program that outputs a lunch menu.Ask the user to enter how much money they have.Ask the user for what menu item they want.Output whether they can pay for it or not.
E.g.Menu:1. Eggs and Bacon £3.202. Vegetable Fritter £2.00
User inputs 3.50User inputs 1Output You have enough for the Eggs and bacon.
User Inputs 3user input 1Output You do not have enough for Eggs and bacon
Create a program that outputs a lunch menu.
Ask the user to enter how much money they have.
Ask the user for what menu item they want.
Output whether they can pay for it or not.
E.g.
Menu:
1. Eggs and Bacon £3.20
2. Vegetable Fritter £2.00
User inputs 3.50
User inputs 1
Output You have enough for the Eggs and bacon.
User Inputs 3
user input 1
Output You do not have enough for Eggs and bacon

