11.9. Chapter Exercises¶
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 be multiplying the weight by price.
Fix 3 errors with indention in the code below so that it calculates the right total for a weight of 0.5 (should be 0.75).
Then change the weight to be 3 (leave numItems as 5).
Write code to complete the isEven
function. It should return True
if number
is
even and False
if it is odd.
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.
Write code to complete the calculatePay
function. It takes in a number of hours
worked and an hourly pay. If the hours worked is 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 recieve
\(40 \cdot 18 + ((50 - 40) \cdot 18) \cdot 1.5\)
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, but has taken at least 90 credits. A Sophomore has taken at least 45. A Freshman 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 "Freshman"
.
Make sure to change the variable, not just print out the answer!