~ Turtle with Python and Tkinter



Learn Python - a great introduction to programming

Welcome to this Hour of code activity, used with permission and sponsored by Trinket.io

You will be learning the programming language PYTHON and you're going to love it!

Today we're going to learn the basics of a programming language named Python. Python is fun to learn but is also a Real programming language that powers websites and apps like Yelp.com and Twitter.

Run your first program!

No need to save the fun stuff until the end. Here's a program in Python. Press Run to see what it does:

At the end of this activity we'll come back to this example and you'll see you'll understand quite a lot about how it works. In fact, you'll write or customize lots of your your own programs in just an hour!

Keep Going!

You'll see lots of new things during this tutorial. Code may always look unfamiliar, and no one knows everything about code, not even the experts! So the most important thing is to keep going, even if you're confused or run into errors!

Also, for each of the interactive examples you see, there is a Reset button in the left hand menu. If you want to start over, it will reset your code back to the beginning.

Have fun!


Lesson 1 - Meet Tina, Move, Say Hello and more ...

Tina is a Turtle that you control with code. Press run to see what this program does, and see if you can figure out what line tells Tina to say,"Why, hello there!"

Don't worry if you don't understand all of the code. You don't have to to get started, and more and more of it will become familiar to you as you keep going.

Use the arrow or click Next to go to the next example!

Moving

As we saw in the last example, Tina can move! When she moves, she draws a line. She can move forward and backward and turn right or left a certain number of degrees.

Run this example to see her move:

She's almost made a square! Can you help her complete it? What other shapes can you help Tina draw?

Saying Hello

Tina's already said hello but you can tell her your name and she'll say hello to you.

First, find the line where Tina says "Why, hello there!". Next, change it so that she's saying hello to you. My name is Elliott, so I'd change what she says to "Why, hello Elliott!"

The program you wrote above is great for people that have the same name as you, but what if someone has a different name? We can write a program that asks for your name with the input function, so that Tina can get it right for anyone's name. Run this program and enter your name:

The input function is what makes the program ask you for your name. Whatever you type in is then stored in a variable. Tina uses this variable to remember and then say your name!

We can teach Tina to say anything we want using input. Can you add input to this program so that anyone who runs it can tell Tina what to say?

Hint: the say_what variable is what Tina says in this program. How can we use input like we did above that the program will ask whoever runs the program what the variable should be?

Color

Tina can change into lots of colors! We can tell her to change into blue by typing tina.color("blue").

Click run to see:

Each color segment in the picture is created by three lines of code:

tina.forward(20)
tina.color("blue")
tina.write("What color am I now?")

Add two more colors to her list by adding another three lines like this but with "blue" replaced with your favorite color. "pink"? "yellow"? "cyan"? It's up to you.

 

Lesson 2 - Tina's pen, grid and going in circles ...

Turtles like Tina have a pen that draws when they move. We can tell them to pick the pen up, so that they can move without drawing a line. Then we can tell them to put the pen down, and they'll draw again. We tell them this with the penup() and pendown() commands.

 

Tina's grid

Tina's world is a grid of squares like the one we sometimes use to graph in Algebra and Geometry.

We can tell Tina to go directly to a specific point on the graph. This makes it easy to teach her to draw something!

The grid goes from -200 to 200 in both directions. You can send Tina to points outside her grid, but then you won't see what she's doing.

Play around drawing with Tina. Send her to other points on the grid with a new line like this:

tina.goto(37,-50)

You can pick whatever numbers you want, but they must be between -200 and 200 for you to see them.

Going in Circles

Tina can make circles of different sizes. Circles make it easy to make funny faces:

Can you make more funny faces in this image?

Turtles can also fill in circles with colors, which can be very helpful for drawing.

 

Lesson 3 - Repeating with Loops and Lists

Lists of numbers

Until now, we've had to write out numbers every time we want Tina to move. We can use a list of numbers and a loop to get her to move many times!

The code number_list = [1,2,3,4,5,6,7,8,9,10] is where Tina gets her numbers from. Can you change the numbers in the list to change how she moves? Make sure that your list starts and ends with square brackets ([]), and has a comma (,) in between every number.

Loops of lists

So far we've been telling Tina to do things over and over. Remember this example?

There's a lot of repetition. If we give Tina a list of colors and tell her to do the same thing with each color, we can make the same picture with much less code!

We can now easily change how many times Tina draws as well. Try adding a new color name to the list. Here's what it would look like to add "yellow" to the list:

color_list = ["black", "blue", "purple", "green", "yellow"]

Don't forget the quotes ("") around your color word!

Hit a snag? Don't worry! Tips:

  • Remember to put your color name in quotes ("")
  • Remember to include a comma (,) in between each color name and the next one
  • Remember to begin and end your list with square brackets ([ and ])

Changing Colors

We can loop over a list of colors and tell Tina to turn that color.

Run this program, and see where the colors come from. What happens if you add or remove colors from the list?

Want to make this program your own? Change what tina does inside the for loop. What else could she do for each color?

Lesson 4 - Turtles are objects!

Turtles are Objects!

Tina is a turtle. But what's a turtle? It's what's called an object. This means that a programmer has written some code that we can use to do cool things. In Tina's case, she knows how to go forward, backward, left, and right because the people who wrote the turtle object thought those would be neat things for turtles to do.

At the beginning of all of our examples, we import turtle. This lets us use code that's already been written and gives Tina and any other turtles we make their abilities.

You can give Tina a different name, and that newly named turtle will know how to do everything Tina does. Give Tina any name you choose in this example:

Hint: You'll have to change all of the names from Tina to the name you choose. Otherwise, Python will be confused which one you're talking about.

Tina and Tommy's Colors

Not only can we rename Turtles: we can have more than one! Let's give Tina a friend named Tommy:

Tina and Tommy are both drawing in black. Can you modify the code tina.color('black') and tommy.color('black') so that they're different colors? Need a color idea? Try an unusal one like 'goldenrod' or 'magenta'. Don't forget the quotes (')

Lesson 5 - If Else Statements

Logical Turtles

Turtles are very logical. They can change what they do based on what we tell them.

Run this program and answer the question. Tina will tell you whether you're right!

What else can you make Tina ask? Try changing the numbers of the math problem.

Tina asks how old you are

Turtles can determine whether a number is above or below another number. Let's write a program that asks how old you are:

Advanced: In the example above you'll see there are try: and except:. These lines try to run code that comes after try:, and if any errors happen, do what's in the except:section. This is how Tina knows whether you put in a real number like 15. This is how real programmers change the behavior of programs based on any errors that might come up.

How High Up is Tina?

Tina can ask us things, and we can also tell her to ask herself things. In this example, we'll tell her to go to a high or low spot on the screen and she'll change what she says depending on how high or low she is.

Here are a few ideas for customizing this program:

  • Try changing what Tina says at the different height levels
  • Try changing what the height levels are
  • Extra Credit: Try changing what Tina does at each different height level. Right now she just writes some words. What else does she know how to do?

More Detail

Want to learn more about how if, elif, and else work? Line 9, tina.pos()[1] asks tina how far up or down the grid she is. elif is short for "else if". Each of the tests of height is tried in order until one is true. If none of the tests are true, the program moves on to the lines indented below else. In Python, else means, "if all else fails, do this". The raise inside the else block raises an error, which means switch to the except block. That's how the program can respond differently if you enter anything except a number in between 200 and -200.

Don't worry about understanding all of this in your first hour! But tools like if, elif, else, raise, and except will give you and other programmers more control over the programs you write as you get more practice.

Lesson 6 - Using Basic Variables and Counters

Lesson7 - Creating a Game Menu and Splash Screen