Skip to content

Homework 3: Branches


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.

Exercise 3.1 Positive or Negative?

If statements can be used to differentiate between different types of numbers. Write a program,, which prompts the user for a number, and then prints out either:

  • NUMBER is negative.
  • 0 is neither negative nor positive.
  • NUMBER is positive.

depending on the sign of the number that was input by the user.

For example,

>>> %Run Enter a number: 35 35 is positive. >>> %Run Enter a number: 0 0 is neither negative nor positive. >>>

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 Here is an example showing the exact prompts and output required:

>>>%Run 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 Name: Leonardo DaVinci Homework Avg: 95.0 Test Avg: 22.3 Lab Avg: 99.5 Course: CT701 CT701 - no such course. >>>%Run 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 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 Sales Tax

In a certain state, sales tax is computed as follows:

  • Everything except food and baby items is taxed at 5%.
  • Food is taxed at 3% for up to $200. Any additional amount above $200 is taxed at 4%.
    • Example 1: The tax for $150 of food is $4.50 (3% of $150).
    • Example 2: The tax for $250 of food is $8.00 (3% of $200 plus 4% of $50).
  • Baby items are taxed at 3.5%.
  • Senior citizens (age 65 and older) get a 50% discount on sales tax, unless they are 90 or over, in which case they pay no sales tax.

In a file named, write a program which prompts the user to enter the following data items, in order:

  • amount: a float value representing the dollar amount to be taxed.
  • category: a string representing the category of the goods to be taxed:
    • the food category is represented as 'food'
    • the baby items category is represented as 'baby items'
    • any other string is permissible, taxed at the base rate of 5%.
  • age: an int value representing the age of the customer in years.

The program should print the customer's sales tax in dollars according to the rules above.


>>> %Run Amount: 100 Category: baby items Age: 46 The tax owed is $3.50 >>> %Run Amount: 53 Category: office supplies Age: 65 The tax owed is $1.33
Note the input prompt and output formatting. Format the tax amount to 2 digits after the decimal point, and place the dollar sign directly next to the amount.

Exercise 3.4 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 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.5 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 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.