Intro to Python Day 3 for Older Kids
Case sensitivity can cause a problem:
Suppose we want to ask the user what his destination is when he is at the airport. Based on his answer we want to tell him he is at the wrong gate or not. So for example, if his destination is Grand Canyon, we will tell him he is at the correct gate, but if his destination is NOT the Grand Canyon, we want to tell him that he is at the wrong gate.
So we could do something like:
dest=input(“What is your destination? #\n”)
if dest == “Grand Canyon”:
print(“You are at the correct gate.”)
else:
print(“Sorry, you are at the wrong gate.”)
What could go wrong??
What if someone typed in grand canyon for their destination? Well, based on the program above, they would be told that they’re at the wrong gate, which is NOT what we at the airport want to happen. We want him to be told he is at the correct gate, but the way we have the code, he will be told that he is at the wrong gate because the string grand canyon is NOT equal to the string Grand Canyon.
Let’s explore upper() or lower().
We can for example, change the user’s answer dest into all upper case or all lower case.
Then in our if statement, make sure we type the words Grand Canyon as either
GRAND CANYON (if we chose to make the user’s answer all UPPER CASE), or type in grand canyon (if we decided to make the user’s answer all lower case.)
Let’s suppose we decided to make the user’s answer all upper case, then our new code could be:
dest=input(“What is your destination? \n”)
dest=dest.upper()
if dest == “GRAND CANYON”:
print(“You are at the correct gate.”)
else:
print(“Sorry, you are at the wrong gate.”)
Exercise:
- Create a program where you define a variable with your age (or someone’s age).
- If the age is less than 16, print out: You can’t drive.
- Otherwise print out, You CAN drive.
Try to write code yourself before looking at a possible answer below.
age = 20
if age < 16:
print(“You can’t drive.”)
else:
print(“You can drive.”)
Exercise:
Create a program where you:
- Ask the person how many minutes she has before her flight.
- If the person has at least 60 minutes before her flight, ask her if she wants to go watch the airplanes take off.
- If the person has less than 60 minutes, tell her to have a safe trip.
Try to write code yourself before looking at a possible answer below.
timeleft=input(“How much time do you have before your flight is scheduled to take off? \n”)
timeleft=int(timeleft) #remember that because we used the input statement, timeleft is a string;
#here we turn it into an integer because we want to compare it to a number later
if timeleft >= 60:
print(“Do you want to go look at airplanes take off with me?”)
else:
print(“Have a safe flight.”)
Challenge:
I want you to create a program where you do the following:
- Ask the person’s name.
- Tell them Hello and use their name and welcome them to the airport.
- Ask their destination
- If their destination is the Grand Canyon, tell them a certain gate to go to.
- If their destination is NOT the Grand Canyon, tell them a different gate to go to.
Try to write code yourself before looking at a possible answer below.
name=input(“What is your name? \n”)
print(“Hello ” + name + ” and welcome to the Raleigh airport.”)
dest=input(“What is your destination? \n”)
dest=dest.upper()
if dest == “GRAND CANYON”:
print(“Go to Gate G1.”)
else:
print(“Go to Gate A1.”)
If, Else If, and Else
What if we have more than 2 comparisons we want made? We can use else if, which is abbreviated elif in the coding.
Suppose we are at a cafe in the airport and want something to drink. Let’s create a program where we are asked what we want
to drink. Then based on the answer we give, we are told the price of the drink. If the cafe doesn’t have the drink we ask for, we’ll print out that
the cafe doesn’t have the drink.
A possible program to do this is:
drink=input(“What would you like to drink? \n”)
if drink == “tea”:
price=5
print(“That will be $” + str(price))
elif drink == “coffee”:
price = 7
print(“That will be $” + str(price))
elif drink == “soda”:
price = 3
print(“That will be $” + str(price))
else:
print(“We don’t sell that type of drink here.”)
So the first comparison was checked. If it had been true, (i.e. if the drink typed in had been tea), then the first print statement would have printed. The other comparisons aren’t checked.
If that first comparison isn’t true, then the next comparison is checked. The comparisons are checked until it finds a true comparison. Once there is a true comparison, what you have in
the indented block underneath that true comparison will be executed. If none of the comparisons are true, then what’s printed under the else will be executed.
Notice the following:
- The if, elif, and else we have left justified.
- At the end of the if, elif, an else statements JUST hit enter and you should get the proper indentation in the Middle section of replit.
- There are colons at the end of the lines with the if, elif, and else
- Also, it’s important to notice that I had to turn the data in price into a string so that I could add it to another string. Previously it was an integer, and you can’t add a string and an integer.
Exercise:
Create a program that does the following:
We are eating at a restaurant before we board the plane. For kids under 5, the cost of a meal is free. For kids 6-12, the price
is $7, and for everyone 13 and over, the price of the meal is $15.
Try to write code yourself before looking at a possible answer below.
A program that asks a customer how old they are and prints off the price based on the person’s age could go as follows:
age=input(“How old are you? \n”)
age=int(age)
if age < 5:
price=0
elif 5 <= age <= 12:
price=7
else:
price=15
print(“Your cost to eat is $” + str(price))
Or you could have done:
age=input(“How old are you? \n”)
age=int(age)
if age < 5:
price=0
elif age <= 12:
price=7
else:
price=15
print(“Your cost to eat is $” + str(price))
The difference between the two programs before is the second comparison. In the second comparison you can just check if the age is less than or equal to 12 versus being 6 and 12 (inclusive) because if the first comparison was false, now checking if the age is less than or equal to 12 will work ok. For example, if the person is 6, the first comparison would be false, but then the 2nd comparison will be true.
Homework Assignment 1:
Create a program where you:
- Ask the user what his destination is.
- If he is going to the Grand Canyon, tell him his gate is G-1.
- If he is going to Florida, tell him his gate is F-1.
- If he is going to Hawaii, tell him his gate is H-1
- Otherwise tell him that he’s at the wrong terminal.
Try to do the homework before looking at a possible answer below.
visit=input(“What is your destination? \n”)
#this turns visit into ALL CAPS
newvisit=visit.upper()
print(newvisit)
if newvisit == “GRAND CANYON”:
print(“Your Gate is GA-1.”)
elif newvisit == “FLORIDA”:
print(“Your Gate is FA-1.”)
elif newvisit == “HAWAII”:
print(“Youre Gate is HA-1.”)
else:
print(“You’re at the wrong terminal.”)
Homework Assignment 2:
Create a program such that:
- You want to show the kids (people 18 years old and younger) who are going to the Grand Canyon a video that showcases all the things kids can do at the Grand Canyon.
- If the traveler is a child AND is going to the Grand Canyon, print out a message that says: Please have a parent escort you to the viewing area for a brief video!
- If the traveler is NOT a child but is still going to the Grand Canyon, print out a message that says: Have a safe flight. It won’t be long before it’s time to board the plane.
- If the traveler isn’t going to the Grand Canyon print out: This plane is not going to your destination.
Try to do the homework before looking at a possible answer below.
Hint: Use the logical operator “and” in your comparisons.
trip=input(“What is your destination? \n”)
trip=trip.upper()
age=input(“How old are you? \n”)
age=int(age)
if trip == “GRAND CANYON” and age<=18:
print(“We have a special video to show to #you. Please have a parent escort you to the #viewing area for a brief video!”)
elif trip == “GRAND CANYON” and age>18:
print(“Have a safe flight. It won’t be #long before it’s time to board the plane.”)
else:
print(“This plane is not going to your #destination.”)
REMEMBER, DON’T JUST COPY/PASTE MY PROGRAM INTO REPLIT AS THE INDENTATION NEEDS TO BE CORRECT. WHEN YOU GET TO THE END OF THE IF, ELIF, ADN ELSE LINES, PRESS ENTER AND IT SHOULD CORRECTLY INDENT IT FOR YOU IF YOU’RE IN THE MIDDLE EDITOR SECTION OF REPLIT. 🙂
If you were NOT able to successfully program the challenges without looking at the answers, also go back and try the challenges.