Today's lab will focus on looping through files and mapping data.
Software tools needed: web browser and Python IDLE programming environment.

Stamping Turtles

We will use two new turtle commands in this lab:

• stamp() which we can use to mark the spot at which the turtle is standing:
tortuga.stamp()

• goto() which we can use to send our turtle directly to a location.
tortuga.goto(10,30)

would send the turtle, named tortuga to the point (10,30).

For example, we can add a stamp() command to our loop that draws a hexagon:

We can also use the goto command to move our turtle directly to a location:

We can add a background image to our turtle programs using the bgpic() command for screens. For example, both challenges below have a background image:

Challenges

• Fill in the trinket below (pseudocode in comments):
• Fill in the trinket below (pseudocode in comments):
• On the map of Lehman College campus, use turtles to mark all the buildings in which you have classes:

Extracting Numbers from a String

When you have a lot of data, it's easier to store it in a file, then to type it all in. Lines of a file are stored as strings (sequences of characters), but we often want to use numbers. This next part of the lab focuses on extracting numbers from strings. Here's our goal:

Write a program that prompts the user to enter a list of numbers. Each number is separated from the next by a comma (','). Your program should then print out the numbers, one per line. Here is a sample interaction:

Please enter your list of numbers:  3, 1, 4, 5, 1, 9

You entered:

3
1
4
5
1
9

Thank you for using my program!

Let's start by breaking down the problem into a "To Do" list:

1. Ask the user for a list of numbers.
2. Separate the inputted string into a list of number
3. Use a loop to print out the numbers.
4. Print a thank you for using the program.

As we've done in past programs, let's fill in the steps:

1. Ask the user for a list of numbers.
To get input from the user, we can use the built-in input() command:

2. Separate the inputted string into a list of number
In lecture, we discussed split() that breaks up a string into a list:
numList = nums.split(',')

3. Use a loop to print out the numbers.
To print out the numbers, we will use a loop:
print("You entered:\n")
for n in numList:
print(n)

4. Print a thank you for using the program.
This is just a simple print statement:
print("Thank you for using my program!")

(See Program 21 on Programming Problem List.)

Accumulator Design Pattern

What if we had wanted to do something more with the numbers? For example, what if we wanted to total them and find out the average. Instead of printing the number in the loop, we would need to:

• Convert each item into a number.
• Add that number to a running total.
• Print out the total at the end of the program.

This template for programs is often called the accumulator design pattern, since a value (in this case, the total) accumulates in a variable.

Let's modify the program above to print out the total (this is very similar to the program written in lecture):

1. Ask the user for a list of numbers.
To get input from the user, we can use the built-in input() command:

2. Separate the inputted string into a list of number
In lecture, we discussed split() that breaks up a string into a list:
numList = nums.split(',')

3. We need an extra step of setting up a variable to accumulate the total:
total = 0

4. Use a loop to total the numbers.
To total the numbers, we will use a loop:
print("You entered:\n")
for n in numList:
num = int(n)
total = total + num

5. Print out the total.
This is a print statement with words (a string) followed by a variable:
print("The total is", total)

Extracting Numbers from a File

Last time, we read strings from a file. We extract numerical data from a file by combining reading from a file with the type conversion functions int() (for whole numbers) and float() (for real numbers).

Here's the outline:

1. Import turtle and set up a map as the background.
2. Set up a turtle to mark points on the map.
3. Open the file of coordinates.
4. For each line in the file:
1. Extract out the numbers for x and y
2. Move the turtle to (x,y)
3. Stamp at that location

Here's an example of mapping points on the Lehman Campus:

Challenges

• Modify the program that the stamps are turtle-shaped.
• Modify the input file so that the turtle also marks the library and student life buildings.

In-class Quiz

During lab, there is an in-class quiz on strings and files. The password to access the quiz will be given during lab.

What's Next?

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.