## Learn Python for Kids Lesson 4

Ready to learn more python?  If you’re new here, start with lesson one.  The second lesson can be found here, and the third lesson here.

## 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:

1. Ask the person how many minutes she has before her flight.
2. If the person has at least 60 minutes before her flight, ask her if she wants to go watch the airplanes take off.
3. 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.”)

## 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:

1. The if, elif, and else we have left justified.
2. 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.
3. There are colons at the end of the lines with the if, elif, and else
4. 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.

## HOMEWORK ASSIGNMENT 1:

I want you to create a program where you do the following:

1. Ask the person’s name.
2. Tell them Hello and use their name and welcome them to the airport.
3. Ask their destination
4. If their destination is the Grand Canyon, tell them a certain gate to go to.
5. 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.”)

## Homework 2:

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.   For the individual who entered their age, print out a sentence telling them their cost to eat.

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.

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.

Recommended Posts