Hardware Reference
In-Depth Information
Using a Text Editor to Create a
Code File
You used a text editor to create code iles in Python in Adventure 4 when using the
turtle module. As you learned in Adventure 4, it makes sense to type all your code
into a text ile using a text editor and save it, before you test that it works by running
it using IDLE. Using a text editor has the added bonus of syntax highlighting , which
works by adding colour to diferent words in your code to make it easier to read. If you
use a command-line editor like nano (which you used in Adventure 2) that does not
have syntax highlighting, you may ind it hard to read a long program. For the projects
in this adventure, you use the Python 3 IDLE text editor as you did in Adventure 4.
In the Python programming language, you can create lists to store data—for example,
you might want a list of names of the students in your class to use in a program that
sends out invitations, or a list of favourite restaurants that your program could sug-
gest when you need an idea for dinner.
he following steps walk you through using a text editor to create a list of objects that
you will use later in your adventure game. In this exercise, you will create a new ile,
add the code to create an inventory, and then save the ile.
For a video that walks you through creating an inventory in Python, select
the Inventory video from the companion website at www.wiley.com/go/
adventuresinrp .
1. Open IDLE 3 and click File from the menu at the top. Select New Window to
open an untitled text ile (see Figure 5-4). Notice that this creates a new text edi-
tor ile, not a shell window, and therefore does not contain a prompt.
2. To save the ile, click File Save As. Navigate to your Documents directory and
name the ile Inventory before clicking Save. If you open your Documents
directory, you can see the ile is now saved there and Python has added a .py to
the end of your ile, so the complete ilename is Inventory.py .
3. In the new ile, type the following:
inventory = [“Torch”, “Pencil”, “Rubber Band”, “Catapult”]
his code creates a list named inventory . Each string, or piece of text data,
represents an item on that list.
4. Underneath the list, type the following:
print(inventory)
Search WWH ::




Custom Search