Today's lab will focus on looping through strings and files.
Software tools needed: web browser and Python IDLE programming
environment.
In Lab 2, we worked through an example from the textbook that changed looped through a list of colors (from 4.6 Iteration Simplifies our Turtle Program):
import turtle # set up alex wn = turtle.Screen() alex = turtle.Turtle() for aColor in ["yellow", "red", "purple", "blue"]: alex.color(aColor) alex.forward(50) alex.left(90) wn.exitonclick()
What if we wanted more colors? We can add more to our list:
for aColor in ["yellow", "red", "purple", "blue", "green", "pink"]:
We can also set up the list ahead of time and then just write its name in the for loop:
myColors = ["yellow", "red", "purple", "blue", "green", "pink"] for aColor in myColors:
Python creates a location in memory that holds the list. If we want to use the list, we just write its name (in this case, myColors). In Python, the lists are items, separated by commas, and surrounded by square brackets (i.e. "[" and "]").
Add in three more colors to make tortuga draw more!
We often store data in a file outside the program. We could store the really long list of colors in a file, and then, we can change the colors in the drawing by just changing the file. For example:
Click on the tabs just below the run button. The first tab has the Python program. The next two contain files. The first is called colorList and is used when you start the program. The second, called colors2 has the first 50 colors from the HTML color list. Change the line that opens the file to:
infile = open("colors2")and run the program again. How many lines does the program draw?
Let's look at this program line-by-line:
import turtle
#Uses a file to store the colors #First open up the file: infile = open("colorList")
#Create a turtle, named tortuga: tortuga = turtle.Turtle() tortuga.shape("turtle")
#For each color in our file, draw a side that color and then turn 93 degrees: for c in infile: tortuga.color(c) tortuga.forward(100) tortuga.left(93)
infile.close()
Our program above worked well since there was only one thing per line. But many files (including data files we will see in later labs) have multiple items per line.
Consider the file that has a color and shape on each line, separated by a comma:
blue turtle red arrow purple classic blue circle red turtle
The first item on each line is the color for the turtle and the second is for the shape. When we read in a line:
for line in infile:
The variable, line, contains "blue, turtle". To split up a string into pieces, we can use a built-in string function: split() that will return a list. To use split(), we need to specify how we want to split up the line. Common ways to deliminate items are commas, tabs, and spaces. Here, we used commas, so, we give split() the input parameter , ' ':
words = line.split(' ')
The variable words is a list. Using the input file above, line, contains "blue turtle", so, words is ["blue", "turtle"]. words[0] contains the color (in this case "blue"). words[1] contains the shape (in this case "turtle"). Python did the work of finding the comma and breaking down the string into parts that we can use.
Let's try it! Copy this file into a Python file:
import turtle #Uses a file to store the colors #First open up the file: infile = open("colorShape") #Create a turtle, named tortuga: tortuga = turtle.Turtle() tortuga.shape("turtle") #For each line in our file, split it in parts, and draw a side that color and shape: for line in infile: words = line.split(' ') tortuga.color(words[0]) tortuga.shape(words[1]) tortuga.forward(100) tortuga.left(93)In the same folder, save the file colorShape. Try running your program. Change the contents of the input file (or create a new one) and try again! Once you've tested your program, submit as Program 18.
If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There's instructors to help you and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.