Yes! Kids can learn PythonChapter 1
Copyright 2018-2020 © Kent Tong (i-Study)
1
Go to https://p4kweb.appspot.com
Click this button.
Step forward, execute and reset
1: Input this.
2: Execute. Then do it once more. 3: Reset the game &
then execute again.
Command, function and call a function
step is a command/function.
To execute a command, you put parentheses () after it.
To call a function is the same as to execute a command.
Exercise: walk to the treasure box
Sample solution● Click the Exec button five times.
Can input multiple commands
They will be executed one by one.
The whole thing is called a program.
Exercise: Walk to the treasure box in one execution
Sample solution
step()step()step()step()step()
What if you spell the command incorrectly?
stop()step()step()step()step()
You will get an error message.
Pick up the treasure box
pickup()
Exercise: Walk to the treasure box & pick it up
Sample solution
step()step()step()step()step()pickup()
What does jump do? Try it
jump()
Change to another background
Choose this background.
Exercise: Jump over the hole & pick up the box
Sample solution
step()step()jump()step()pickup()
What does walk(3) do? Try it
Argument
walk(3)
3 is called an argument here. It doesn't mean arguing with someone. It is the information you hand to the command.
Exercise: Call walk & then pick up the box
Sample solution
walk(2)jump()step()pickup()
Not every function accepts an argument
walk(2) step(2)jump(3)
Perform calculation
walk(1+2)walk(3-1)walk(2*3)
What does print do? Try it
print(123)print(2*6)print(2*(3+1))print(10/2)
It outputs the values
The output goes to here.
Calculate & print the result● A slice of cake costs 6 dollars. A bottle of soda costs 5 dollars. How much do
they cost in total?
Sample solution
print(6+5)
Use variables to show the meanings of numbers
print(6+5) cake=6soda=5print(cake+soda)What does 6 mean?
cake=6 creates a variable
cake=6soda=5print(cake+soda)
This line creates a variable named "cake" and its value is 6.
cake
6
A variable must have two things: a name & a value
cake=6soda=5print(cake+soda)
cake
6
(name)
(value)
Exercise: Draw a diagram to show the variable soda
cake=6soda=5print(cake+soda)
Sample solution
cake=6soda=5print(cake+soda)
soda
5
Looking up the value of a variable
cake=6soda=5print(cake+soda)
soda
5
cake
6
What if there is a typo in the variable name?
cake=6soda=5print(coke+soda)
soda
5
cake
6
Can't find a variable named "coke".
You will get the "not defined" error
When creating a variable, a value is calculated first
a=2+6print(a)
The value is calculated first (8 in this case).
Then, let the variable point to that value.
a
8
Draw a diagram to show the variable b
b=3*5print(b)
Sample solution
b
15
Can use a variable to store the result
cake=6soda=5print(cake+soda)
cake=6soda=5total=cake+sodaprint(total)
Exercise: How many variables are created below? Draw a diagram to show eachcake=6soda=5total=cake+sodaprint(total)
Sample solution: three
soda
5
cake
6
total
11
cake=6soda=5total=cake+sodaprint(total)
Calculate & print the result while using variables● A slice of cake costs 6 dollars. You pay 10 dollars. What is the change?
cake=6soda=5total=cake+sodaprint(total)
For reference
Sample solution
cake=6payment=10change=payment-cakeprint(change)
Draw a diagram to show the variables below
cake=6payment=10change=payment-cakeprint(change)
Sample solution
cake=6payment=10change=payment-cakeprint(change)
payment
10
cake
6
change
4
Calculate & print the result while using variables● A slice of cake costs 6 dollars. How much are 3 slices of cake?
Sample solution
cake=6slices=3total=cake*slicesprint(total)
Calculate & print the result while using variables● 3 bars of chocolate cost 12 dollars. How much is a bar of chocolate?
Sample solution
total=12bars=3chocolate=total/barsprint(chocolate)
Exercise: Jump onto the platform & get the box
Choose this background first.
Sample solution
walk(2)jump()walk(2)pickup()
Exercise: go upstairs & get the box
Choose this background first.
Sample solution
walk(4)jump()walk(4)jump()walk(4)pickup()
Exercise: Use a variable to represent 4walk(4)jump()walk(4)jump()walk(4)pickup()
_____=4walk(_____)jump()walk(_____)jump()walk(_____)pickup()
Sample solution
width=4walk(width)jump()walk(width)jump()walk(width)pickup()
the width of the staircase