Exercise 1: Hello Turtle
Course 67101 -- Introduction to Computer Science, HUJI, Semester A 2021 Due: 20/10/2021 at 22:00
Objectives
Practice working with functions and the turtle module. Additionally, practice using the print function.
Setup
Import the turtle library at the top of your file hello_turtle.py:
import turtle
Important Turtle Functions
| Function | Description |
|---|---|
turtle.forward(number) | Move the turtle forward by number steps |
turtle.left(deg) | Turn the turtle left by deg degrees |
turtle.right(deg) | Turn the turtle right by deg degrees |
turtle.up() | Lift the pen -- movements will not be drawn |
turtle.down() | Lower the pen -- movements will be drawn |
File Structure
- Parts A-D: write in
hello_turtle.py - Part E: write in
math_print.py
You must write documentation (comments) for your code. For example:
# These next lines draw a triangle
Document each function with a docstring inside triple quotes.
Example
def intro_example():
"""This is only an example method for printing hello"""
print("hello")
Part A: Drawing a Petal
Implement the function draw_petal which draws a single petal by performing these steps:
- Move forward 30 steps
- Turn right 45 degrees
- Move forward 30 steps
- Turn right 135 degrees
- Move forward 30 steps
- Turn right 45 degrees
- Move forward 30 steps
- Turn right 135 degrees
Part B: Drawing a Flower
Implement the function draw_flower which draws a single flower:
- Turn left 45 degrees
- Draw a petal (call
draw_petal) - Turn left 90 degrees
- Draw a petal (call
draw_petal) - Turn left 90 degrees
- Draw a petal (call
draw_petal) - Turn left 90 degrees
- Draw a petal (call
draw_petal) - Turn left 135 degrees
- Move forward 150 steps
Calling this function alone may draw the flower upside down, since the drawing depends on the initial angle. After completing Part D, the flowers will be drawn correctly.
Part C: Drawing a Flower and Advancing
Implement the function draw_flower_and_advance which draws a flower and moves the turtle to enable drawing additional flowers:
- Draw a flower (call
draw_flower) - Turn right 90 degrees
- Lift the pen (
turtle.up()) - Move forward 150 steps
- Turn right 90 degrees
- Move forward 150 steps
- Turn left 90 degrees
- Lower the pen (
turtle.down())
Part D: Drawing a Flower Bed
Implement the function draw_flower_bed which draws three flowers:
- Lift the pen
- Move forward 200 steps
- Turn left 180 degrees
- Lower the pen
- Draw three flowers by calling
draw_flower_and_advancethree times
Running the Functions
Use the following format at the bottom of your file:
if __name__ == '__main__':
draw_flower_bed()
turtle.done()
Part E: Print Practice (math_print.py)
In this part, write functions that practice using the math module. Add at the top of math_print.py:
import math
def sin_30():
print(math.sin(30))
def tan_50():
print(math.tan(50))
def cos_86():
print(math.cos(86))
Functions to Implement
golden_ratio-- prints the golden ratiosix_squared-- prints the result of 6^2hypotenuse-- prints the length of the hypotenuse of a right triangle with sides 12 and 5pi-- prints the value of pie-- prints the value of esquares_area-- prints the areas of squares with side lengths 1 through 10 (ascending order), separated by spaces
For the task "print perimeters of squares with side lengths 1 to 3":
def squares_area():
print(4*1, 4*2, 4*3)
Running the Functions
if __name__ == "__main__":
golden_ratio()
six_squared()
hypotenuse()
pi()
e()
squares_area()
Do NOT hardcode the results (e.g., printing -0.988... instead of using math.sin(30)). This will fail the manual check and lose all points.
Part F: Running Tests (Pre-Submission)
After your first submission, you will receive feedback indicating a failed test. To complete this part:
- Open the feedback file
- Find the required filename and string
- Create a new file with that name
- Add a function called
secret_functionthat prints the specified string
The \n character at the end of the string in the feedback is automatically added by print -- you do not need to include it explicitly.
Submission Instructions
Files to Submit
hello_turtle.pymath_print.py- The file containing
secret_function(from Part F)
Creating the ZIP File
Create a zip file named ex1.zip containing exactly the three files above.
Linux:
zip ex1.zip hello_turtle.py math_print.py
Windows: Select the files, right-click, choose "Send to" then "Compressed (zipped) folder".
After creating the zip, extract it to a separate folder and verify all files are present and not empty using ls.
Write the names of students you consulted with as a comment at the top of hello_turtle.py.