Meeting 2020-01-21

Python Variables

Now that we have the software installed and are a little familiar with the Minecraft+Python environment, we are going to start learning about Python programming. Launch Minecraft and IDLE (the Python editor and shell) and arrange then on your screen so you can see all three windows. We will be entering Python statements in the "Python 3.8.0 Shell" window to see how Python works, and then we'll switch to the Python editor for our Minecraft project.

Python Variables

One of the first concepts that almost all programming languages have is variables. A variable is a named box that hold data and makes it available for use in your program.

Why use variables?

- Allows the same program code to work with different data values

- Place to store the results of calculations and operations on data for later use

- Way to refer to the same data in multiple places in your program

- Way to document what different data is called and used for

- Easy way to send groups of data from one part of your program to another

- Way to set options and parameters in programs (constants)

Compared to other programming languages, Python is pretty relaxed about setting up and using variables. To create a variable, we just pick a name and assign a data value to the name with an equal sign. Enter this in the Python Shell window:

mycoolvariable = 5

You have created a variable and given it a value of 5. If you enter just the variable name, Python will print the value for you:

Python will keep track of this value until it is changed or deleted or the Python window closes. A variable in a program is stored only until the program ends - if you want to keep it longer than that you have to save it somewhere (like a file on your hard disk).

You can change your variable's data value by executing another assignment statement:

>>> mycoolvariable = 10

>>> mycoolvariable

10

The original value is gone for good!

Variable Names

Many programming languages have similar rules about how variables can be named. Variable names in Python:

    • Can be combinations of upper and lower case letters, numbers, and underline

    • Upper and lower case letters aren't the same - MyCoolVariable is not the same as mycoolvariable

    • Can't start with a number, but can have numbers elsewhere

    • Can separate multiple words with an underline but not a space (this is Python style): my_cool_variable

    • Shouldn't be the same as built-in Python commands and statements (letters will turn colors in the shell)

Try out some of these ideas in your Python Shell window:

If you enter something Python doesn't like, you'll get a red error message. These are not always super helpful but they should tell you what the error was and where it happened.

Types of Data

In the examples above we stored numbers like 5 or 10 in the variables we created. Like most programming languages, Python supports different types of data in variables. Here is a list of some of the built-in types:

    • Integer numbers (like 1,2,3 and -10 and 12345678901234567890)

    • Floating point numbers (like 3.14159 and -0.0000001)

    • Boolean values (True and False, must be capitalized!)

    • Complex numbers (like 1+2j) - useful in engineering but we won't talk more about these!

    • Text strings (like "hi there!" or 'hello world')

    • Sequence types like list, tuple, and range

    • Mapping (dict) and set types (set)

    • Binary types (byte and bytearray)

We will mostly be using integer and floating point numbers and strings at first, but the more complex types can be very useful in certain situations!

Python has a bunch of different built-in functions to do things with the different data types. We will talk more about the different types later.

Unlike some languages, Python doesn't require you to tell the program the type for each variable - it tries to figure out the right type to use from the data value. Python also allows you to change the type of data a variable stores just by running a new assignment statement:

You can see what type Python thinks a variable is by using the type(varname) function.

Project: Rewind Position

For our Minecraft project we will write a Python program that teleports you to some position in the Minecraft world, delays, and then moves you back to your spawn point. We will set the position and the time delay using variables in the program.

Enter the following program in an IDLE editor window. You can cut and paste it but you will learn more if you type it in and correct your mistakes!

from mcpi.minecraft import Minecraft

import time

mc = Minecraft.create()

playerX = -1234

playerY = 33

playerZ = 1234

delay_time = 10

mc.postToChat('Moving you somewhere')

mc.player.setTilePos(playerX, playerY, playerZ)

time.sleep(delay_time)

mc.postToChat('Back to spawn!')

mc.player.setTilePos(0,4,0)

Save the program to %appdata%/.minecraft/mcpipy/lowelltele.py (or pick your own name) and in Minecraft enter "/py lowelltele" to run it.

Try changing the values of the playerX, playerY, playerZ, and sleep_time variables and running it again. If your program gets stuck you can end it by entering "/py" (no program name). What happens if you set a variable to a floating point value like 1234.567? How about a string value?

The coordinates you provide are relative to the spawn point, not the absolute coordinates you see when you press F3. So x,y,z=(0,0,0) is your spawn point. The "Y" coordinate is the height above the ground. Depending on your terrain, teleporting you to Y=0 may put you underground. So, I increased it to Y=4.