Intro to Python for Older Kids Day 2
So you’ve learned about the print function, input function, and string concatenation. Let’s make use of all of these concepts.
Challenge 1:
We are at the airport, ready to go to the Grand Canyon. It’s a self-check in at the airport. Create a program:
- Where you ask the person’s name and store the information as a variable.
- Ask the person’s destination and store it as a variable.
- Greet the person by name.
- Tell them you hope that they have a nice time in their destination, and use the destination’s name.
Try the challenge before looking at a possible answer below.
name=input(“What is your name? \n”)
dest=input(“What is your destination? \n”)
print(“Welcome ” + name ” to the Raleigh-Durham airport.”)
print(“We hope you have a nice time in ” + dest)
Challenge 2:
We are at the airport, ready to go to the Grand Canyon. It’s a self-check in at the airport. Create a program:
- Ask the traveler how many bags they are bringing with them on the trip.
- Charge them $25 for every bag they are bringing
- Print out a sentence that tells how much they are being charged.
Try the challenge before looking at a possible answer below.
bags=input(“How many bags are you bringing on the airplane? \n”)
charge=25*int(bags)
print(charge)
print(“It will cost you $” + str(charge) +”.”)
Comparison Operators:
Let’s talk about 6 comparison operators:
> means greater than
>= means greater than or equal to
< means less than
<= means less than or equal to
== means equal to
!= means not equal to
We can use comparison operators if we want to compare things.
Use TWO Equal signs NOT one when we Want to Compare
Notice that when we did things such as creating a variable, we used ONE equal sign. When we want to make
a comparison we will use TWO equal signs.
Comparing Variables that have been defined as Numbers
Let’s create some variables and compare them.
For example, try the following:
a=2
b=2
c=3.
If you type a == b in the CONSOLE section and press ENTER, a value of True will be returned BECAUSE the variable a IS equal to the variable b.
To see this in the Middle editor section, type: print(a == b).
Try checking other conditions. If you type a > c in the Console section, a value of False will be returned BECAUSE the variable a is NOT greater than the variable c.
Again, if you are in the middle section, type: print(a > c)
Type b != c
A value of True will print out because b is NOT equal to c. This condition is True.
Comparing Strings
We can also compare strings. For example type “Andre” == “Anna” in the console section. A value of False will be returned because these two strings are NOT equal.
Now try typing “Andre” != “Anna”
Now a value of True will return BECAUSE the string Andre IS not equal to the string Anna. This is a true statement.
What will be returned if we were to type: “STEM” == “stem”
A value of False will be returned because of the case sensitivity of python. These two strings are NOT equal to each other.
A value of False will be returned because of the case sensitivity of python. These two strings are NOT equal to each other.
Using the If Statement
We can use the if statement to have the computer do something based on a certain condition.
For example, we can do the following:
name=input(“What is your name? \n”)
if name == “Shenek”:
print(“She’s the Alston kids’ mom”)
What happens is:
- First it’s going to print out the words What is your name? The user can then input his or her name.
- It is then checked to see whether or not what they inputted (defined as the variable name) is equal to Shenek.
- If what they inputted is equal to the string Shenek, so that the condition is true, then the words She’s the Alston kids’ mom will be printed out.
If name is NOT equal to Shenek, NOTHING will be printed out because the condition was not true.
BE CAREFUL WHEN WRITING AN IF STATEMENT:
- Make sure you use TWO equal signs if you want to compare if two things are equal.
- Make sure that you put colons at the end of the if statement.
- The print statement needs to be indented. If in the middle section of replit, after typing the if statement JUST hit ENTER, and it should indent correctly for you.
Case sensitivity:
What if we had typed in “shenek” as the answer to the input question. What would have happened?
NOTHING will be printed out because the statement will NOT be true. Only if the answer is Shenek will something be printed out. Remember in python Shenek is NOT the same as shenek.
In another lesson, we will talk about how to make something print out even if the first condition is not true.
Exercise:
- Define a variable age equal to any number you desire.
- Use the if statement to compare age to the number 16.
- If the age is less than 16, print out You can not drive.
Try to figure out the code yourself before looking at a possible answer below. 🙂
age=10
if age < 16:
print(“You can not drive.”)
Try running this as it is. Also, go back and change the age to 16 or a number greater than 16. For example, if you set age to 20, NOTHING will happen
because the statement age < 16 will NOT be true anymore.
Challenge:
- Ask someone their favorite ice cream flavor.
2. If it’s the same as your favorite, print out the words That’s my favorite flavor too!
Try to write code yourself before looking at a possible answer below.
myfav=”cherry”
yourfav=input(“What’s your favorite ice cream flavor? \n”)
if yourfav == myfav:
print(“That’s my favorite ice cream flavor too!”)
AGAIN IT’S IMPORTANT THAT YOU DO NOT JUST COPY/PASTE MY CODE ABOVE INTO REPLIT. You want it to be indented correctly. So manually type in my code if you want to try it out, and AFTER you type the line with the if statement on it, just hit ENTER, and you will get the correct indentation if you’re in that middle editor section.
Then you can type in the line with the print function. So manually type in the code. 🙂
ANOTHER POSSIBLE ANSWER:
Notice how I defined a variable myfav and then put that in my if statement. I did NOT have to define a variable. I could have instead put in my favorite ice cream in the if statement as such:
yourfav=input(“What’s your favorite ice cream flavor? \n”)
if yourfav == “cherry”:
print(“That’s my favorite ice cream flavor too!”)
NOTICE THE DIFFERENCES IN THE TWO PROGRAMS:
- When I defined a variable myfav I did NOT put quotes when I used it in the if statement. So we don’t need quotes when we use a variable here.
- When I instead put in cherry in the if statement I needed to surround the string with quotes.
We talked about using the if statement. We can use the if statement when we want to compare things. However, when you use the if statement, only if the statement is true does anything “happen.” That can be somewhat frustrating. What if you want something to print out or “happen” even if the first statement is NOT true.
We can use the if AND the else together.
Using If and Else
What if we want the computer to print something out EVEN if the first condition is NOT true? We can use if and else together.
Let’s do the following:
apples = 5
grapes = 8
if apples < grapes:
print(“Apples cost less than grapes”)
else:
print(“Apples cost more than grapes”)
So what happens is that IF the value assigned to apples is less than grapes (the statement is True), then the statement Apples cost less than grapes will print out.
However, if that condition is False, then the statement Apples cost more than grapes will print out.
One of the statements WILL print out, either the first statement or the second statement.
BE CAREFUL:
- Make sure you put colons at the end of the if line and at after the word else.
- The two print statements need to be indented. Again, when you are on the line above those statements, JUST hit enter and it should properly indent it for you in replit if in the middle section.
Homework Assignment 1:
- You’ve met someone at the airport in the lobby. Ask them where they are traveling to.
- If they are traveling to the Grand Canyon, print out that that’s where’s you’re traveling too.
Try the homework before looking at a possible answer below.
visit=input(“Where are you visiting? \n”)
visit=visit.upper()
if visit==”GRAND CANYON”:
print(“I’m going there too!”)
#Remember how to get the correct indentation. This may not be the exact indentation needed on the print line.
Homework Assignment 2:
- Create a program where you ask a person: What is the security code?
2. If the user’s security code is NOT equal to 500, then print out a message that tells them that they inputted the incorrect code.
REMEMBER: When we use the input function, the data that user inputs will be a string. So be careful when trying to do your comparison in the if statement.
Remember that you can turn a string into an integer. 🙂
Try the homework before looking at a possible answer below.
code=input(“What is the security code? \n”)
if int(code) != 500:
print(“You entered the wrong code!”)
Homework Assignment 3:
- Create a program where you ask What day is it?
- If it’s Monday, print Happy Monday.
- Otherwise print It’s not Monday.
Try the homework before looking at a possible answer below.
day=input(“What day is it? \n”)
if day == “Monday”:
print(“Happy Monday.”)
else:
print(“It’s not Monday.”)