HW 4

Download and save the file hw04.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 as a result in the shell area:

Q1 - testA: 0
Q1 - testB: 0
----------------------
----------------------
----------------------
----------------------
----------------------
----------------------
----------------------
----------------------
----------------------

There are 10 questions total - each worth 1 point. The questions are written in comments. Each question involves you implementing a function below it to solve a specific problem. For example,

# Q1 [1pt]. Write a selection statement (if statement) that sets the value
# of a variable named answer to 1 if the input parameter is equal to 100.
# Set answer to 2 otherwise. Return the variable answer.
def q1(input):
    # your code here
    answer = 0
    return answer

I suggest you work one question at a time. Read the question description and write your solution. Before you move on, test that your solution is correct by uncommenting all lines of code which call the function at the bottom of the program. The first one is uncommented for you.

To help you check that your program produces the correct result, here is a sample output from my answer key. Your finished program should output the same once all your code is correctly added:

Q1 - testA: 1
Q1 - testB: 2
----------------------
Q2 - testA: 4
Q2 - testB: 4
Q2 - testC: 3
Q2 - testD: 2
Q2 - testE: 1
Q2 - testF: 0
----------------------
Q3 - testA: True
Q3 - testB: False
Q3 - testC: True
----------------------
Q4 - testA: 36.3
Q4 - testB: 38.0
----------------------
Q5 - testA: 30
Q5 - testB: 3
Q5 - testC: 3
Q5 - testD: 5
Q5 - testE: 5
----------------------
Q6 - testA: 6.283185307179586
Q6 - testB: 18.84955592153876
----------------------
Q7 - testA: 3.141592653589793
Q7 - testB: 28.274333882308138
----------------------
Q8 - testA: 4.1887902047863905
Q8 - testB: 113.09733552923254
----------------------
Q9 - testA: 82.5
Q9 - testB: 340.0
Q9 - testC: 403.75
----------------------
Q10 - testA: 82.5
Q10 - testB: 540.0
Q10 - testC: 553.75

Grading

You will be graded based on the quality of your code and that the output of each function matches the expected result. I will be checking to see that you correctly computed the solution programmatically.

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, or missing answers. 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 calculate the gross income for an employee, but instead it accidentally computes more or less money to be paid than what it should, 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 file hw04.py updated with your changes to Moodle.