HW 2

Create a new folder in CS100 called hw. Download and save the file hw02.py to your CS100/hw folder.

This file is a template. It contains some code there for you. Put your name at the top of the file. Run it to test that it works. You should see the following in the python turtle graphics window:

There are 6 questions total. They are indicated in the code by the notation

### Q1 [point value]
### Description of question

All questions involve modifying a function in some way which is commented out. In order to test that your function works as expected, uncomment the function call and run your program.

Here is a visual of the solutions so you can use it to compare against to check that your answers are correct.

Question 1

Modify the drawSquare function to draw a rectangle whose horizontal width is twice the vertical width. When you are finished, you should see:

Question 2

Create a new function drawRectangle that takes three parameters: myTurtle, width, and height. Uncomment the call to drawRectangle. When you are finished, you should see:

It’s okay if the black rectangle is located somewhere else, such as inside the red one.

Question 3

Modify the function question3. Use the range function to print out all numbers from -10 to 10. Uncomment the function call to question3. In the Thonny Shell area, you should see the following (in addition to the turtle graphics).

Answer to Q3: All numbers from -10 to 10
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10

Question 4

Modify the function question4. Use the range function to print out all numbers from 10 to -10. Uncomment the call to question4 to test it. In the Thonny shell, underneath your answers to question 3, you should see the following (in addition to the turtle graphics).

Answer to Q4: All numbers from 10 to -10
10
9
8
7
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10

Question 5

The drawDoubleSpiral function definition only takes one turtle and draws one spiral. Modify the function to use a second turtle and create two spirals in opposite directions. Uncomment the call to drawDoubleSpiral to test it. You should see the following now:

Question 6

Modify the function question6. In it, create a turtle to plot the function y = (1/3)x - 300. Plot using x-coordinates from -100 to 100. Uncomment the call to question6 to test it.

You should see the following picture, now with the line shown below the red rectangle:

Grading

You will be graded based on your written program and that the output matches what is expected. In order to receive any credit, your program must run without syntax errors in Thonny. Thonny reports syntax errors in red in the shell area; if Thonny outputs an error, your code isn’t usable. Code that doesn’t run is just text - it’s not a program. It’s an important part of programming and problem solving to come up with a solution that can be shared with others.

Your program doesn’t have to be perfect to be eligible for credit! It’s okay to have some semantic errors. Semantic errors don’t stop your program from running, but they cause your program to produce an unexpected or incorrect result. For example, if your program is supposed to draw a square, but instead it draws a rectangle, that would be a semantic error. Semantic errors may result in partial credit depending on how close your solution is.

How to submit

Double check that your program runs without syntax errors. Submit the updated file hw02.py to Moodle.