13.2. Week 2 Lab

Material Covered

Basic interactivity, turtles, operands, basic functions (Chapters 1 & 2)

13.2.1. Level 1

Your objective in this level is to complete the code below so that a turtle draws an octagon on the canvas, like this:

../_images/octagon.png

Tip

You are given some code to start with, do not edit these lines!

  1. Create a turtle variable

  2. Create a variable for the side length of an octagon, and ask the user to input a length (don’t forget the conversion)

  3. Calculate a turn angle and store this in a variable. An octagon has eight sides, meaning you will have to turn your turtle eight times. Do some basic division to figure out by how many degrees your turtle will have to turn after each side.

  4. Make the turtle draw the octagon using the side length variable and the angle variable. Note: This part will be very repetitive, but we will learn a way to avoid retyping the same commands in a few weeks

13.2.2. Level 2

Building upon your program from level 1, your objective is to calculate the area of the octagon the turtle drew and print out the area to the console.

Tip

Octagon area = the length of a side squared, multiplied by 2, multiplied by (one plus square root of two)

Tip

The function to get a square root is: math.sqrt()

  1. Copy your code from Level 1 into the active code window above.

  2. Create a variable and assign to it the area of the octagon, using the formula in the tip above for the calculation

  3. Print out the value to the console, using the following format:

../_images/output_format.png
  1. Make sure your formula is correct by cross referencing your answer with the answer you get using a calculator

13.2.3. Level 3

Below is a program similar to the type created in level 1 and 2: first a turtle draws a square, then the area of the square is printed out in the console. Objective: Convert this code into a generalized program that uses user input to set the length of the sides of the square. Use this input value to draw the square and to calculate and report the area of the square.

Tip

You only need to add one line of code, the rest is just editing the code that is there.

You have attempted of activities on this page