Computer Science LearnITWithMrC ⛯ Year 7 Year 8 Year 9 GCSE
Responsive image

More Funky Functions

What am I Learning today?

I am learning how to use python turtle to further develop an understanding of repetitions (loops) in programming and be able to use for loops in creating patterns.
I am learning how to use python turtle to further develop an understanding of functions in programming and be able to create functions that accept multiple parameters.

Knowledge Organiser

×

Lesson

Task 1 - Getting Started Click to see more

You are going to screenshot all of your work into a Powerpoint file.

To download the PowerPoint by clicking on the image below

To save your powerpoint go to: File explorer -> Year 9 Common Area:S -> You can save here -> Computer Science -> Term 1 -> Lesson2

Save the Powerpoint file: as your name


  • Complete Task 1 in your Powerpoint


  • Task 2 - Parameters 101 Click to see more

    Func-y Town:

    When we wrote our square function, the code looked like this… .

    # This is a function called drawsSquare
    def drawSquare(): 
        for myMoves in range(4):
            tim.forward(100)
            tim.left(90)
    

    • What if we wanted to be able to choose the size of our square?
    Lets try the following tasks:

    Tick each task when you have successfully done it in the following list:

    Complete the following tasks

    Go to your your turtle4 code
    Copy the drawSquare function into your code
    Add a parameter inside your drawSquare function brackets called sideLength - drawSquare(sideLength):
    Also add sideLength inside of the brackets of tim.forward - tim.forward(sideLength)
    Make the colour 'hotpink'
    Add a loop that runs 8 times like we did last week so we can create our square pattern - for myMoves in range(8):
    Click on Run then Run module or press F5


    This is what your code should look something like

    • By adding sideLength to the first line, we're telling the function that when it runs, it will be provided with a number, which the function will be able to refer to as sideLength.
    • In computer science, this is called passing in a parameter to the function. When you use print() in Python, you pass in text (a string, as we say). When you use tim.left(90), you're passing in a whole number (an integer)
    • This is handy, as we can now make squares of any size, and make ever nicer patterns. Change your 'for' loop at the bottom of the program
    • From:

    for myMoves in range(8):
        drawsquare(50)
        tim.left(45)
    
    • To:

    squareSize=50
    for myMoves in range(8):
        drawsquare(squareSize)
        squareSize+=10
        tim.left(45)
    

  • Screenshot your python code and output into your Powerpoint

    • Experiment with different ideas. You could…

    Extension task:

    Tick each extension task when you have completed it in the following list:


    Make a pattern where there the the number of steps (I used 8) comes from user input.
    Write a function where the number of sides is a parameter
    Create a function that takes a colour for the shape to be drawn
    Anything else you can dream up!


  • Screenshot your python code and output into your Powerpoint


  • Task 3 - More Parameters Click to see more

    Lets go:

    • Passing in one parameter is useful. It's even better if you pass in several.Look at the code below and see how we have added the newColour parameter:
    import turtle
    
    # Set up the window and its attributes
    wn = turtle.Screen()     
    # Choose our background colour. US spelling.
    wn.bgcolor("white")      
    # Window title
    wn.title("Turtle Power")
    
    # Create tim and set some attributes
    tim = turtle.Turtle()   
    tim.pensize(2)
    
    def drawsquare(sideLength,newColour):
        tim.color(newColour)
        for x in range(4):
            tim.forward(sideLength)
            tim.left(90)
    
    for myMove in range(10):
        # Create the square with a size and a colour.
        drawsquare(50,"red")  
        tim.left(360/10)
    
    
    # Wait until the window is closed.
    wn.mainloop()    
    

    This is what your code should look something like

    • You'll notice now that rather than using drawsquare(), we added the size we wanted inside the brackets instead, as well as the colour we'd like the square to be. This can give us the ability to create more intricate patterns, and eventually to start mixing up the colours.

  • Screenshot your python code and output into your Powerpoint


  • Task 4 - What can you do Click to see more

    • Look at the patterns below see if you can create them using the code you have learned so far.
    • We can do this by using functions and loops.
    • If you want to change the colour try looking at Task 5 - Lists.

  • Screenshot your python code into your Powerpoint


  • Task 5 - Lists Click to see more

    Sometimes, it's handy to store several pieces of information so that we can access them all when we need them. For this, we use a list.

    Type these commands into a new Thonny file one at a time to get the hang of manipulating lists:

    
    someColours=['Red','Blue','Pink','Black']
    
    print(someColours[0])
    someColours.append('Green')
    
    print(someColours)
    
    someColours.sort()
    print(someColours)
    
    someColours.reverse()
    print(someColours)
    
    someColours.pop()
    print(someColours)
    
    someColours.pop(1)
    print(someColours)
    
    
    
    
    

    Code it in Turtle

  • Using lists, we can get more flexibility over the patterns we can create.
  • Use the code below to experiment with going through a list of coulours using a function
  • import turtle
    
    wn = turtle.Screen()
    wn.bgcolor("white") 
    wn.title("List Practice")
    
    tess = turtle.Turtle()
    tess.pensize(5)
    
    def colouredSquare(sideLength,newColour):
        tess.color(newColour)
        for x in range(4):
            tess.forward(sideLength)
            tess.left(90)
    
    colourList=['red','green','blue','orange','hotpink','purple']
    
    for eachColour in colourList:
        colouredSquare(75,eachColour)  # Draw a square in the current colour.
    
        # The len() function will tell you how long a list is, or how many characters are in a string.
        # I'm using it here to calculate the angle I need, based on the size of the list.
        tess.left(360/len(colourList))
    
    wn.mainloop()
    
    

    See if you can use lists to help you create the shapes with different colours in task 4


    Task 5 - Lesson Review/Homework Click to see more


    Summing it all up

    Lets look at the learning outcomes and decide which one best describes our current level of understanding :

    Tick the one you feel is closest to your level

    Learning Outcomes I need to learn how to use python turtle to further develop an understanding of functions in programming and be able to create functions that accept multiple parameters.

    • I have a basic understanding of how I can use python turtle to further develop an understanding of functions in programming and be able to create functions that accept multiple parameters. with a little help from my teacher
    • I can show my teacher that I can use python turtle to further develop an understanding of functions in programming and be able to create functions that accept multiple parameters. without their help.
    • I can use python turtle to further develop an understanding of functions in programming and be able to create functions that accept multiple parameters. independently and I can also explain it to others and can complete any extension tasks I am given.

    🠜 Now update your learning objectivesClick on the Assessment image



    My Notes: Coding_Turtle

    Student_Comment_1 not found

    Task Notes/Comments - Add here Click to see more

    Comments/Notes

    Copyright © 2013 - 2025 LearnITwithMrC