Skip to main content

Exercises 23.9 Chapter Exercises

1.

The code below is supposed to set the price to 1.5 if the weight is less than 2 and otherwise set it to 1.3. It then calculates the total by multiplying the weight by the price.
Fix 3 errors with indention in the code below so that it calculates the right total for a weight of 0.5 (the answer should be 0.75). You will not pass the tests yet.
Then change the weight to 3 and run the program again to pass the tests (the answer now should be 3.9).

2.

Write code to complete the isEven function. It should return True if number is even and False if it is odd.
Make sure to return False or True, not "False" or "True" with quotes. False in Python means False/no/0. "False" means “the word False”. True in Python means True/yes/1. "True" means “the word True”.
To check if a number is even, you can divide it by 2 and look at the remainder. (Remember that x % 2 says “divide x by 2 and get the remainder”.) If the remainder is 1, the number is odd. If the remainder is 0, it is even.

3.

Write code to complete the calculatePay function. It takes in the number of hours worked and an hourly pay rate. If the hours worked are less than 40, it should just return the hours times the hourly pay. If more than 40 hours were worked, the person should earn normal pay for 40 hours and 1.5 times the normal pay for the hours over 40. So if someone worked 50 hours, at $18 per hour they would receive \(40 \cdot 18 + ((50 - 40) \cdot 18) \cdot 1.5\)

4.

The number of credits someone has determines what class level they are at a University. A Senior has taken 135 or more credits. A Junior hasn’t taken that many credits, but has taken at least 90. A Sophomore has taken at least 45 credits. A First Year is anyone who has taken 45 or less.
Write code to complete the getClass function. Your code should use credits and the above logic to change classLevel to the appropriate value: either "Senior", "Junior", "Sophomore", or "First Year" .
Make sure to change the variable, not just print out the answer!
You have attempted of activities on this page.