Intro to Python for Older Kids Day 1 Part 2

These are notes for Day 1 part 2.

 

learning python coding for kids

Changing Data Types

Going from an Integer to a String

What data type we are dealing with can make a difference.  You may encounter a situation where you are dealing with more than one data type.  If for example, you are dealing with an integer but we want to use it as a string, we can change its data type.

For example, suppose we have a variable minutes defined such that: minutes = 10.

Suppose we want to use this information as a part of a string concatenation.  

You may want to print out a statement that takes whatever is stored in the minutes variable and concatenate it to a string.

However, if we did something like:

print(“You have ” + minutes + ” left.”)

This would cause an ERROR though because the data stored in minutes is an integer and you’re trying to “add” it to a string.

Before we “add” it we can change the data in minutes to a string.  There is more than one way this can be done.

We could do:

minutes=10

minutes=str(minutes)  #here were are changing the data type to a string

print(“You have ” + minutes + ” left.”)

 

OR we can do:

minutes=10

print(“You have ” + str(minutes) + ” left.”)  #we can convert it to a string inside of our print function

learn python coding for kids

 

Homework Assignment 1:

Purchase 2 items (for example, apples and bread.  Give the cashier some money, and then figure out your change.  Print out the change received using a SENTENCE.  So don’t just

print out the numerical number of the change, but say something such as “Your change is $15.”

Hint:  Don’t forget that you may need to change the data type of the variable you have the change amount stored in before doing string concatenation.

Try the homework before looking at a possible answer below.

kids learning python coding

Possible answer:

apples=7

bread=2

total = apples+bread
print(total)
change=20-total
print(change)
print(“Your change back is $” + str(change))  #we needed to turn the data stored in the variable change into a string before “adding” it to another string

learn python coding for kids

How to Ask a User to Input Information

Some of the topics we’ve talked about are some of the data types in python, naming variables, string concatenation, and using a line break in python.

Today, we are going to talk about the input function.  We used the print function to output things, but what if we want to ask a user to input some information?

Remember, we are going to the Grand Canyon. Hooray!!! I hope that you want to go with us.  What if we wanted to ask someone if they wanted to go with us?

Using the input function we can ask our friend if she wants to go with us, and the computer will wait for her to input her answer.  

It’s a function, so we’ll use parentheses.

So for example, to create a situation where a user is asked if she wants to go with us AND the computer will wait for her to input the information, type the following:

input(“Do you want to go?”)

The computer is now waiting for someone to input an answer.  So we were able to have the computer print out something WITHOUT using the print function.

If we wanted there to be a space between the question mark and the user’s answer, put a space between the question mark and the ending quotation mark:

input(“Do you want to go? “)

 

learn python coding for kids

 

Using the Line Break inside the input function

What if we wanted the person’s answer to be on the next line?  How would we accomplish this?

We can type:  input(“Do you want to go? \n”)

learn python coding for kids

 

Exercise:

Using the input function, ask the user What city do you live in?

Try the exercise before looking at a possible answer below.

kids learning python coding

 

input(“What city do you live in? \n”)

 

learn python coding for kids

 

Using a Variable and the Input Function

It’s nice that we can ask someone some information and she gets to input that information, but what if we want to recall that information later?

We can create a variable and set it equal to our input function.

For example to store our friend’s answer to recall later we can do

answer=input(“Do you want to go? \n”)

Then type: print(answer) to see the information recalled.

Using the Input function and String Concatenation

Let’s ask someone’s age.  Then let’s print out something telling him his age in a sentence.

age=input(“What is your age? \n”)

print(“You are ” + age ” years old.”)

Notice, how I spaced things so the words printed out nicely in the last sentence.

Why was I able to do this string concatenation? It’s because when you use the input function, the data is a string.

Importance of Knowing what Data Types you have

What if we were to ask the user’s age, and then we wanted to calculate how old he’d be in 10 years?

We could something like this:

num1=input(“What is your age? \n”)

age_in10=num1+10

This last statement will cause an ERROR?  Why??

It turns out that when you use the input function, the data is a STRING.  So the variable num1 is a string.

Then when we created age_in10 we tried to add together the string num1 and the integer 10.  We can’t do this.  

First, let’s change num1 to an integer before trying to add it to the number 10.

So try something like:  age_in10=int(num1) + 10.

Then do print(age_in10) to see the result.

learn python coding for kids

Homework Assignment 2:

  1. Create a program where you ask the user how much he paid for his plane ticket to the Grand Canyon.
  2. Then calculate what half of the cost would be.
  3. Then tell him you have a special offer where you will be giving him back the calculated amount of money.

Try the homework before looking at a possible answer below.

 

kids learning python coding

 

cost=input(“How much money did you pay for your plane ticket to the Grand Canyon? \n”)

#we turn the data that’s in cost into an integer because it was a string BUT we want to do a mathematical calculation using it

cost=int(cost)

#before we try to do a string concatentation, we change moneyback into a string

moneyback=cost/2

moneyback=str(moneyback)

print(“We have a special offer where you will be getting back $” + moneyback + “!” )

 

Recommended Posts

Leave a Comment

0
Pinterest
fb-share-icon
Instagram
learn python coding for kids