First Steps With Python

Marty's First Steps with Python

The quickest way to learn about Python is to start coding and Marty is a great way to make Python code come to life.

The first things to do are to check if you already have Python installed on your computer, install the martypy library and make sure you can open a Python Shell. There's a detailed guide to doing this here so please follow the guide if you haven't already done so and then continue below when you have installed martypy.

You will also need to connect your Marty to WiFi and find its IP address). Your computer on which you are running the Python code has to be connected to the same network as your Marty.

Make Marty Dance

Only three lines of Python are required to make Marty dance:

Remember: Disconnect the app before running the code below; WiFi and Bluetooth cannot be used at the same time._

import martypy
# USB connection
# change "COM1" on this next line to specify the USB port you want to use
# comment this line out to use WiFi
my_marty = martypy.Marty("USB")

# WiFi connection
# uncomment this and change the IP address to your Marty's to use WiFi
# my_marty = martypy.Marty("wifi", "192.168.0.5")
my_marty.dance()

Notice that a few lines looked more like English and less like Python. It is a _comment_. In Python all the text on a line after a hash (#) is ignored. So if you are copy-pasting from the snippet at the beginning of the section, feel free to copy the comment too or leave it out. Comments only provide additional information for humans; computers completely ignore them.

You can type the lines into a Python Shell one by one. Let’s take a look at what each line does:

Line 1: Import martypy

Controlling a Marty is not part of the Python programming language which is why there is the martypy library. Like any other library, martypy is a collection of code that uses whatever is available in Python to make it as easy as possible for other programmers to perform a niche task - in this case controlling Marty.

Martypy Idle Ex1 Line1

Line 1 simply tells Python that you want to use the martypy library.

Line 2: Establish connection

Martypy Idle Ex1 Line2

my_marty is a variable name. Variables are a familiar concept in most programming languages (including Scratch) and they are just a named place to hold some information that you might want to use again in the future - for instance the scores in a game might be variables called your_score and my_score.

In this case we are using my_marty to refer to Marty itself, so we say so by making my_marty equal-to (using the equals sign) the Marty that we imported in the line above.

Now, because we’re writing this program to run on a separate computer than Marty itself, we need to tell the computer how to talk to Marty so that Marty can be told what to do. The two things in the brackets are enough information to let the computer talk to Marty. The first one ("wifi") tells your computer what method to use to talk to Marty, in this case WiFi. The second argument ("192.168.1.15") is the IP address which tells your computer where on your WiFi network to find your Marty. This will often change so use the app to check Marty's current IP) before you start coding. For example, you may notice that I had to change "192.168.0.42" from the example at the beginning of this section to "192.168.1.15" which is the IP that was assigned to my Marty on my network this time.

Another method of connecting to Marty is via USB. To connect your Marty to your computer use the USB C port on the back of Marty and plug it into your computer. Instead of using "wifi" in the connection line use "USB" so it looks like this my_marty = Marty("USB").

Line 3: Dance!

Martypy Idle Ex1 Line3

This one is probably self explanatory. When you have a variable which refers to a thing like Marty you can use the dot (.) as a way to send a message to the thing telling it what you want it to do.

Marty can do a lot more than dance and all of these capabilities are available in the same way. If you haven't closed IDLE yet, you can even keep using your my_marty variable and send it more messages - you only need the first two lines at the beginning of each Python Shell session or Python program (we will get to programs later).

So let’s now get to the promised Marty's First _Steps_.

Make Marty Walk

First Step

Making a step is very similar to the dancing example:

import martypy
# USB connection
# change "COM1" on this next line to specify the USB port you want to use
# comment this line out to use WiFi
my_marty = martypy.Marty("USB")

# WiFi connection
# uncomment this and change the IP address to your Marty's to use WiFi
# my_marty = martypy.Marty("wifi", "192.168.0.5")
my_marty.walk()

If you still have your Python Shell open, you can skip the first two lines and just type in the last one (my_marty.walk()). You should see Marty take two steps forward. We will usually leave out the first two lines in subsequent examples; just remember to execute them first whenever you open a new Python Shell.

Martypy Idle Ex2 1

More steps

Sending a message after every 2 steps would be quite tedious if you need your Marty to cover some distance. Luckily, there is a way to tell Marty to walk farther. The following tells Marty to make 10 steps:

my_marty.walk(10)

Martypy Idle Ex2 2

Most messages like “walk” can accept more detailed pieces of information in the brackets behind them. These pieces of information are called _arguments_. We say that “walk” accepts the number of steps as its first argument.

Short steps

Another argument that “walk” accepts is the step length, but this one is written a bit differently. For example, we can make Marty take 5 very short (8mm) steps:

my_marty.walk(5, step_length=8)

Martypy Idle Ex2 3

Challenge: Can you use the step_length argument to make Marty walk backwards?

Task Runner