Python Functions
Functions
Functions allow you to group code together and use it more than once. You have to:
1. Create the function
2. Call ( use) the function.
1. Create the function
2. Call ( use) the function.
Step 1: Create the Function |
Step 2: Call the function |
Use the keyword def and give the function a name. This should include a verb and summarise its purpose. In the example below the function draws a square, so it is called DrawSquare. The function below will draw a square but only when it is called - see step 2.
|
Call the function to make it do something.
|
Example: Create a function and call it twice.
The following code shows the function and it being called twice to make two squares.
Create a Function. Call it many times passing in different values.
Example: 50 Circles
With just a few lines of code 50 circles are drawn on the canvas, each a little bigger than the last.
A Complete Python Tutorial
Click here and try out some of the other commands.
Watch the following tutorial to find out more. Try out the code in Python Sandbox.
Draw the USA Flag in Python Turtle
Watch the following video for an example of how to drag the flag for the USA. This is for advanced Python turtle programmers.
Tutorial Sites
Advanced Graphics
Click here to learn how to create more advanced shapes.
Draw a House with Python Turtle
Create a Racing Game
Click here to learn how to create a racing game.
Python - Text Mode
Although it's great for learning professional programmers would not use Python turtle. They would use a text editor to write a console program ( one that just uses text). Here are some of the basic commands.
Display Text
The print command will display text:
print ("hello")
print ("hello")
Get Text from the User
Variables are used to store data. In the code below the variable username will store the name typed in by the user. To display data from the variable, type the variable name.
username = input("Enter username:")
print("Username is: " + username)
username = input("Enter username:")
print("Username is: " + username)
In the code below there are two variables: order and quantity. Input is used to get data from the user. Once the data is stored it can be used. For example below print is used to display variable name.
Using IF
- Use IF to check for a certain value. Look at the example code below. When the user types in their order, it is stored in the variable called order.
- On line 10 IF is used to check if the order contains the value 1. If it does then the user ordered a cheese burger. So a message is displayed saying "You have ordered a cheese burger".
- If the user types in 2 then 2 is stored in the variable called order.
- On line 13 IF is used to check if order contains 2. If it does the message "You have ordered a hot dog" is displayed.
Calculating Total Price
If we already have variables storing the price and the quantity we are ready to calculate the total price. The code below multiples price by quantity and stores the result in the variable total.
- Anything typed into the program is stored as text. So quantity must be cast ( converted) into a number first.
- Since total is a number we have to cast it to a text before it can be displayed.
- Note: if you are interested casting is changing the data type by adding a data type and round brackets. You can find out more here: Python Casting (w3schools.com)
Calculations - Using Operators
The following code will add two numbers together and display the answer:
print(10 + 5)
The code above only works for the numbers 10 and 5. We call this hard coded because the numbers are always the same. It is better to store the numbers in variables so that they can be changed. The following code will add and then display the two numbers stored in the variables a and b.
a = 3
b = 5
print(a + b)
You could also store the result ( 8 in this example) into a separate variable:
a = 3
b = 5
result = a + b
print( result)
print(10 + 5)
The code above only works for the numbers 10 and 5. We call this hard coded because the numbers are always the same. It is better to store the numbers in variables so that they can be changed. The following code will add and then display the two numbers stored in the variables a and b.
a = 3
b = 5
print(a + b)
You could also store the result ( 8 in this example) into a separate variable:
a = 3
b = 5
result = a + b
print( result)