Showing posts with label python turtle. Show all posts
Showing posts with label python turtle. Show all posts

Thursday, 23 May 2013

Programs to build (Python Turtle)

  1. Change the colour of the pen
  2. Draw a star with 12 points
  3. Draw a square using a for loop
  4. Draw a star using a for loop, fill in star in with colour
  5. Ask a user how big they want their square to be, then draw it
  6. Ask a user how big they want their star to be, then draw it - add this into program number 6
  7. Ask the user what shape they want, how big they want it to beand what colour they want it to be, then draw it
  8. Allow the user to define their own shapes
 

Wednesday, 3 October 2012

Shapes with User Input (Python)

Python Turtle is the drawing package which comes with Python.
Task: Create a program which allows a user to input a shape, what size they wanted it to be and what colour they wanted it to be.
The program should then draw the shape for them!

You needed to solve this programming problem by yourself using the knowledge you already have in python (using turtle, defining shapes, for loops, variables) as well as figuring out how to add in an "if statement".

What is an "if statement"?
An "if statement" is where the program to checks one thing against another. For example, if the variable shape has the word "star" saved in it, the program needs to run the bit of code that draws a star.

Here is what an "if statement" looks like:
if x == 3:
 print("X is equal to 3")

(The two equals signs means that Python is checking something. One equals sign means that the variable has been assigned that value.)

TASK/HINT: You need to amend your program to add in a number of "if statement" which checks what a user types in against a criteria. If the user has typed in a specific word and it meets the criteria then Python will run a bit of code.

If you go to my Google Docs the Python Turtle help sheet file is uploaded here.
If you go here you can look at Snake Wrangling for Kids (for Windows), or here for Mac. This explains about "if statements".


Monday, 1 October 2012

Python Turtle

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.