Intro to Python Day 4 Older Kids
What if within a single comparison we want to check to see that more than one thing is true or we want to check that at least one thing is true? First, we’ll talk about the logical operators and and or. We will later talk about the logical operator not.
When using the logical operator and, every condition listed has to be true in order for the statement to be considered True.
For example, suppose we have:
In the above example, the first statement is true because a is equal to be AND a is less than c. If either one of those had been false, the second statement would have printed out.
if a == b and a > c:
print(“Everything is true”)
else:
print(“Everything is not true”)
In this 2nd code, the first statement is NOT True because while a is equal to b, a is NOT greater than c.
With the logical operator or, only one condition in your statement has to be true in order for statement to be True. It’s fine if all conditions are true but it’s not necessary.
So a statement is False only if ALL the conditions are False.
NOT logical operator example:
Here the number stored in the variable cookies IS EQUAL to the number stored in doughnuts. So then when we do NOT True, we get False. So the if statement here is False.
Create a program such that:
We want to board the airplane.
1. If you have a FIRST CLASS TICKET OR you are a family with children who are 5 years old or younger OR you are 65 or or older you will be allowed to board FIRST.
2. If you have a SECOND CLASS TICKET and you are a family with children who are 6 years old and older, you will board 2nd
3. Everyone else will board last.
So if a family has some children then they will board first or second. They will NOT be among those boarding last. Only the second class people with NO children
will board last.
Try the exercise above before looking at a possible answer below.
You can have if statements within another if statement.
Suppose we are an adventure company out in the Grand Canyon area. Our goal is to book people for one of our tours. It takes at least 2 days to do a tour, so a person needs to have at least 2 days left of his trip. There are two options: a shorter tour and a longer tour.
Example program:
Notice that if the FIRST if statement is true (meaning the person said yes to wanting to go on a tour AND they have at least 2 days left on their trip), then the 2nd if statement is evaluated (It’s checked whether the person wants the short tour.) If that FIRST if statement is False, then the 2nd if statement will not be evaluated. If the FIRST if statement is False, then the statement I hope you enjoyed your visit to the Grand Canyon will be printed out.
Homework assignment:
Make a Pick Your Own Adventure Game. You can try to write code for the pick your own adventure game I describe below or make up a new pick your own adventure game.
My game is as follows:
- Ask the user what activity they want to do: four wheeling, rock climbing, or kayaking
- If they pick four wheeling or kayaking, tell them GAME OVER.
- If they pick rock climbing, ask them if they want to go left or right.
- If they pick left, tell them GAME OVER.
- If they pick right, tell them they’ve found the fossil and tell them good job.
Try the homework before you look at a possible answer below.