12.2. Week 2 Lab¶
Basic interactivity, turtles, operands, basic functions (Chapters 1 & 2)
12.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:
You are given some code to start with, do not edit these lines!
Create a turtle variable
Create a variable for the side length of an octagon, and ask the user to input a length (don’t forget the conversion)
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.
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
12.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.
Octagon area = the length of a side squared, multiplied by 2, multiplied by (one plus square root of two)
The function to get a square root is: math.sqrt()
Copy your code from Level 1 into the active code window above.
Create a variable and assign to it the area of the octagon, using the formula in the tip above for the calculation
Print out the value to the console, using the following format:
Make sure your formula is correct by cross referencing your answer with the answer you get using a calculator
12.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.
You only need to add one line of code, the rest is just editing the code that is there.