Hardware Reference
In-Depth Information
A
return
is a way of passing back a value from a function when it jumps back to
the program that used the function in the first place.
For example, when you ask for a random number, you use the function
random.
randint()
. This function returns a value, which you can store in a variable like
this:
a = random.randint(1,100)
. The
return
in Python just allows you
to use this same technique of passing back a value from your own functions.
The
raw_input()
function reads a line of text entered at the keyboard. If you put
a string between the brackets, that string is used as a prompt, so
name = raw_
input("What is your name?")
will both ask a question and get the response.
When you use
raw_input()
to read from the keyboard, it always returns a
string of text. If you want to enter a number (for example, in your menu system),
you will have to use the
int()
function to convert it to a number. Some
Python programmers like to do this all on one line, like this:
age = int(raw_
input("What is your age?"))
.
Throughout this topic you have been using Python version 2. If you use another
computer it might have Python version 3 installed. There are a few differences
between the two, and one of these is that in Python version 3, you need to use
input()
instead of
raw_input()
as used in Python 2. This topic uses Python
2 because the Minecraft API is written to work with Python 2 only and would
require some small modifications to make it work with Python 3.
DIGGING INTO THE CODE
You have used
boolean
variables before in other adventures, but it is worth looking
at how the
anotherGo
variable is used in the
duplicator.py program
. Here
are the important parts:
anotherGo = True
while anotherGo:
choice = menu()
if choice == 8:
anotherGo = False
continued