Lesson 2

Turtles with User Input


In this program, we’re going to set the size, speed and color through user input

Our “input” function gives us back a string. However, we’re going to need to set our turtle’s size and speed with a whole number (integer)

If we tried to set the turtle’s speed or size using a string instead of a number, we would get an error, so we need to change it from a string to an int.

This process, of taking a string and making a number out of it, is called parsing.

On line 8 we will set up our first input, speed. Add the following code: speed = int(input(“Please enter the speed of the turtle: “))

We will need to set up two additional inputs. One for the Speed, and one for the Color. Try to code these yourselves, using line 8 as a model. Compare your code to what’s shown on lines 9 and 10 in the image on the side

When we test this code, the terminal should ask for our input

Working with inputs


Now that we have our inputs set up, we need to incorporate them into how our turtle will function.

On line 12add the following code: t.speed(speed). This will make it so that whatever speed we put in as our input, the turtle will use that speed. On the next two lines, try setting up the same thing, but for Size and Color. Once you are done, check your work with the code shown on lines 12-13