Turtle is Python's drawing package. It is a lot like Logo but much more sophisticated.
Spelling, punctuation and spaces all matter!
If you get an error message you need to read it and try to figure out why it doesn't work! Usually the error message will tell you what line your problem is on (Although sometimes this might mean that the code on the line before that is wrong...)
This help sheet was made to get you started with the code.
Importing Turtles
In Python, the first thing you have to do is import the Turtle, otherwise Python doesn't know that you want to draw something.
To do this you write
import turtle
IMPORTANT: This always has to go at the top!
Drawing Shapes
To draw a shape you need to tell Python that you want to move the Turtle (think of the Turtle as being the arrow...). You can go forward (up), backwards (down), left or right but you need to tell Python how far you want the Turtle to move or turn.
turtle.forward(50)
The code above will move the Turtle forward 50 pixels.
For Loops
Programmers are lazy - what is the point in copying and pasting the same bit of code 5 times?
This is where for loops come in.
A for loop is where you tell the program to repeat something a certain number of times.
for x in range (1,5):
turtle.forward(100)
turtle.right(90)
The code above will repeat 4 times (you have to add an extra number onto the end). What shape will it draw?
Indentation
Python is very particular about the indents you put.
By indenting a bit of code you are saying that it "belongs" to the code above it. So in the for loop example, the forward and right turn belong to the for loop (Anything that isn't in the for loop needs to be aligned with the for x in range part of the code).
When you indent you should keep consistent.
It is recommended that you indent by pressing the tab key once, or pressing space twice.
Whichever you decide to do you need to stick with it, or Python might get mardy!
Defining a Shape
This is where you say that a bit of code has a title, and it can be used (or "called upon") many times.
def mystar:
for x in range(1,13):
turtle.forward(100)
turtle.right(130)
The code above will not run unless you tell Python that you want it to run though.
To run the code you type
mystar
This is useful if you want to draw a star many times, you could put the mystar bit of code in a for loop!
Setting Variables and User Input
A variable is a place where data can be saved. As the name suggests a variable can vary, so it might be that the data in it changes throughout the program.
User input is when the user interacts with the program in some way, usually by typing something in.
When the user types something in the program needs somewhere to save what they have typed in, and this is where variables are useful!
size = int(input("Please input the size you want your shape to be: "))
The code above says that the value of size is set to whatever number the user types in.
Size is a variable. The number that the user types in might not always be the same (therefor it varies...). This is where the number is saved.
Int means integer, which means that the program will only accept the user typing in a number.
Input means that the program is expecting the user to type something in.
The message in the quotes "" is just to help the user out, so they know what to do.
Using the variable in your program
In order to use the variable and let people choose how big they want their shape to be, the program needs to pass the number into the code which sets up how long the edges of the shape are.
import turtle
def mystar(size):
for x in range(1,13):
turtle.forward(size)
turtle.right(130)
size = int(input("Please input the size you want your shape to be: "))
mystar(size)
This code has been altered slightly from the examples above.
After we have set up the definition of the star, we need to tell Python that we want to include the variable size when we are drawing the star. In the part of the code which draws the star, the size of the sides has been replaced by the variable size.
The program runs, and the first thing which happens is that the user is asked to input a size for their shape (remember anything in "def" wont run until we tell Python to run it. If we never tell Python to run it then it will never get used!), then once the user has input their size, the mystar section of code is run, with the variable size being passed into that bit of code.
No comments:
Post a Comment