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.
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
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.")
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?
Code:
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:
#Change line 13 to ask for a different type of chocolate bar.
# Use another nested if. Yes for that chocolate, else give them nothing.
print("Okay... have a bounty bar then.")
Code 2:
#Try running this program and get as many different outputs you can.
print("Think of an animal. I can guess your animal with a few guesses.")
legs = input("Do your animal have 4 legs? y/n")
if legs == "y" or legs == "yes":
whiskers = input("Does your animal have whiskers? y/n")
if whiskers == "y": #Can you make it so it accepts "yes" as well?
print("Your animal is a cat!")
else:
print("Your animal is a dog!")
else:
legs = input("Does your animal have legs at all? y/n ")
if legs == "y":
wings = input("Does it have wings? y/n ")
if wings == "y":
print("It's a bird!")
else:
#Change the line below to ask another question - such as if it has a tail
print("It must be a monkey ")
else:
print("It is a fish!")
print("I am really Good, aren't I?")
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....
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 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 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
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 "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.")