Lesson1 Aguidetolearningpythonwithmblock
Lesson1 Aguidetolearningpythonwithmblock
Step 2:
ensure that 'sprites' is selected in the toolbar
(As you walk through this tutorial it is a good idea to add each line
of python code as you go along)
What are we going to do?
This means your first line of Python code, when working with a
sprite in mblock, will always be:
from mblock import
Adding an event
Using blocks, the Therefore we need to include
is accessed using the events this in the Python code.
toolbar in mblock An event is being imported from
mblock:
from mblock import event
Adding an event
The event in this case is the Python also requires that the
green flag event is defined, so it is
understood.
This is communicated as
This is communicated in Python follows:
as follows: def on_greenflag():
@event.greenflag
sprite.say('Hello')
sprite.say('Hello')
Your panda sprite should show a think bubble with 'hmm' inside
when you click on him
Things to remember:
• The event in this case is clicked
• The event will need to be defined
• The speech is hmm
(This Python script follows the same format as the code in the tutorial)
Control blocks
The is a control block.
The question will be asked by the sprite, meaning an answer will be provided. This needs
to be reflected in the Python code
*Remember it is a new block so indentation matters!*
answer = sprite. input('What colour is the sky?')
Breaking down the blocks
Next, we want the sprite to react to the answers given to the question, 'What colour is the
sky?'
So, if the answer is blue (which is the colour of the sky), the sprite should do something
== is a comparison
operator, it checks for
true and false
This is expressed as follows:
if sprite.answer == 'blue' :
*indentation is not
necessary as the 'answer ='
block is within the 'if ,then'
block
Breaking down the blocks
If the answer to the question 'what colour is the sky?' is blue, we want the sprite to say 'Good job!'
This is expressed as :
sprite.say('Good job!')