Skip to content

Homework 3: Branches

Objectives

The goal of this assignment is to gain experience working with:

  • Relational operators (<, >, ==, etc.)
  • Boolean operators (and, or, not)
  • Conditional statements

Input Validation

On HW3, you don't need to check for invalid input, except where explicitly noted. For example, if the program expects an integer, you can assume the user (autograder) will enter an integer.

We are using two platforms

The first 20 pts are on Runestone. Look for the HW3.1 assignment on Runestone. Answer the questions (repeatedly if needed) until you get them all correct.

The next 30 pts are on Gradescope. There are three separate "assignments" – one for each exercise below (HW3.2, HW3.3, HW3.4). Resubmit each program until you get full credit.

Docstring and PEP 8 checks

For full credit, your Gradescope submissions must pass all formatting checks. Please read the Installing Thonny, etc. and Docstring Requirements pages for more details.

HW3.1 Concept Questions

Complete the "HW3.1" assignment on Runestone. It consists of 12 multiple choice questions and 4 mixed-up code exercises.

Exercise 3.2 Grade Calculator

You have professor that doesn't trust Canvas to compute final grades correctly, but, for some reason, she is going to trust you to write a program to do it. Please don't let her down!

She wants to be able to enter a student's name, the course id, and their homework, test and lab averages as floating point numbers. The weights of each grade component are different in each course:

Course Homework Test Lab
CT201 30% 50% 20%
CT222 45% 40% 15%
CT301 20% 25% 55%

After calculating the final grade, the program should print Final grade for NAME in COURSE is GRADE, but if the course id entered is not one of the 3 given in the table, the program should print COURSE - no such course (where NAME and COURSE are the entered values, and GRADE is the calculated value). The final grade should be printed with 2 digits after the decimal point.

Your program file should be named grade_calc.py. Here is an example showing the exact prompts and output required:

>>>%Run grade_calc.py Name: Raffaello Sanzio Homework Avg: 100.0 Test Avg: 94.32 Lab Avg: 88.0 Course: CT301 Final grade for Raffaello Sanzio in CT301 is 91.98 >>>%Run grade_calc.py Name: Leonardo DaVinci Homework Avg: 95.0 Test Avg: 22.3 Lab Avg: 99.5 Course: CT701 CT701 - no such course. >>>%Run grade_calc.py Name: Leonardo DaVinci Homework Avg: 95.0 Test Avg: 22.3 Lab Avg: 99.5 Course: CT222 Final grade for Leonardo DaVinci in CT222 is 66.59 >>>%Run grade_calc.py Name: Michelangelo Simoni Homework Avg: 85.0 Test Avg: 93.3 Lab Avg: 24.6 Course: CT201 Final grade for Michelangelo Simoni in CT201 is 77.07 >>>

Exercise 3.3 Steel Grade

Steel is one of the most useful materials in the world. The grade of steel aids in determining its usefulness. Assume the grade of the steel is based on hardness, carbon content and tensile strength. The following conditions are used to assign a grade:

  1. Hardness must be greater than 50
  2. Carbon content must be less than 0.7
  3. Tensile strength must be greater than 5600

The grades are assigned as follows:

  • Grade is 10 if all three conditions are met
  • Grade is 9 if conditions 1 and 2 are met
  • Grade is 8 if conditions 2 and 3 are met
  • Grade is 7 if conditions 1 and 3 are met
  • Grade is 6 if only one condition is met
  • Grade is 5 if none of the conditions are met

Write a Python program named steel.py that accepts three input values: the hardness, carbon content, and tensile strength. Hardness is an integer, carbon content is a float from 0.0 to 1.0, and tensile strength is an integer. Your program should report the grade of the steel based on the three inputs.

Your input and output should match this exactly, substituting the appropriate values.

Enter hardness: 40
Enter carbon content: 0.7
Enter tensile strength: 5089
Grade: 5

Exercise 3.4 Pet Age Calculator

Our pets have shorter lifespans than we do, so it is interesting to understand how old they are in human years. People used to use a simple formula for calculating how old a cat or dog is in human years - the pet's age times 7. Now veterinarians believe that has it is more complicated - cats and dogs (and even particular breeds) age differently, and they age faster when they are younger. (How to Calculate Dog Years and Cat Years)

You are going to write a pet age calculator that takes this new research into account, using the following information:

Dog Years Human Years
0 0
1 12
2 24
3 and up add 4 years to the previous year's age
Cat Years Human Years
0 0
1 15
2 24
3 and up add 4 years to the previous year's age

Your program should prompt the user for their pet's type, using the prompt, "Type: ", and their pet's age, using the prompt, ""Age: ". If the user enters a pet type other than "cat" or "dog", the program should print "Pet type not recognized". Otherwise, it should print the pet's age in human years using the format shown in the samples below.

Your program file should be named pet_age.py. Here are several examples showing the prompts and output format required:

Type: dog
Age: 10
Your dog's age in human years is 56.
Type: cat
Age: 12
Your cat's age in human years is 64.
Type: dog
Age: 1
Your dog's age in human years is 12.
Type: cat
Age: 1
Your cat's age in human years is 15.
Type: parrot
Age: 18
Pet type not recognized.

HINT: To help you test, the website linked above has tables that give the human age for most possible pet ages.

Reflection (required)

Submit the graded survey on Canvas after completing the assignment.