# Chapter 3 Exercises¶

1. Write down what you think each of these would print, then use the active code window to check your results:

1. 9 * 5

2. 2 / 5

3. 5 % 2

4. 9 % 5

5. 6 % 6

6. 2 % 7

7. 3 / 0

1. 9 * 5 = 45

2. 2 / 5 = 0.4

3. 5 % 2 = 1

4. 9 % 5 = 4

5. 6 % 6 = 0

6. 2 % 7 = 2

7. error - you can’t divide by zero

2. Insert the correct operators in place of the # s so each line prints True. Remember == checks for equality.

3. Add a set of parentheses to the expression x = 6 * 1 - 2 so that the code below prints -6 instead of 4.

Add the parentheses around the 1 - 2 as shown below.

4. Add parentheses to x = 12 * 2 - 3 + 4 * 2 so that it prints -4 instead of 29.

Add the parentheses around the 2 - 3 and the 4 * 2 as shown below.

5. Complete the code on lines 3 and 5 below to print the cost of a car trip of 500 miles when the car gets 26 miles per gallon and gas costs 3.45 a gallon. It should print 66.3461538462.

Calculate numGallons as the miles / milesPerGallon. Calculate total as numGallons * pricePerGallon.

6. If Sunday is represented by 1, Monday by 2, Tuesday by 3, etc., and today is Sunday, complete the code on line 4 (with a math expression) to show what day it will be 82 days from today (it should print 6 which represents Friday)

7. Complete the code on lines 4 and 5 to print how many miles you can drive on $25 if your car gets 40 miles per gallon and the price of gas is$3.65 a gallon. It should print 273.97260274.

Calculate numGallons as funds / pricePerGallon. Calculate numMiles as milesPerGallon * numGallons.

8. Fix the syntax errors.

The variable should be on the left and the value should be assigned should be on the right, and there should be no spaces in the variable name.

9. Complete the code on lines 3 and 7 to print the final cost for an item that is priced $68, but is 40% off the original price and you have a coupon to take an additional 20% of the sale price. It should print 32.64. Calculate saleReduction as price * amountOff. Calculate couponPrice as salePrice - couponReduction. Show Comments 10. Fix the syntax and semantic errors so that the answer is 1 instead of 3.5 The variable should be on the left and the value should be assigned should be on the right. Also, change the / to % Show Comments 11. Finish the code on lines 4 and 5 to print how many wings you can buy if you have 5 people and they each can spend$4 a person and the wings are $0.50 a wing. It should print 40.0. Calculate total as numPeople * amountPerPerson. Calculate numWings as total / price. Show Comments 12. It is currently 10:00, complete the code to tell what time it is going to be in 123 hours (12-hour time, not 24-hour time) (Answer should be 1) The time on the clock can be represented as a modulus of 12. Show Comments 13. Finish the code on lines 2 and 3 in the code below to print how many hours and minutes you have been waiting when you have been waiting a total of 270 minutes. Remember that there are 60 minutes in an hour. It should print 4.0 and then 30. Calculate numMinutes as totalMinutes % 60. Calculate numHours as (totalMinutes - numMinutes) / 60. Show Comments 14. You’re buying groceries and your sub-total is$73, but you have to pay 7% tax. Complete the code to find your total price. Total should be 78.11

The total should be the subTotal plus the subtotal times the tax.

15. Fix the syntax errors in the code below so that it calculates and prints the number of hours you will need to work if you earn $8 an hour and want to earn$100. It should print 12.5.

Change the first line to payPerHour = 8. Change the third line to numHours = amount / payPerHour.

16. Complete the code to show how many minutes are in 1.3 days and how many seconds are in 1.3 days. It should print 1872.0 and 112320.0

Number of minutes is the number of hours times 60 and seconds is the number of minutes times number of seconds.

17. Finish lines 5 and 6 in the code below to print how many apples you can buy when apples cost 0.60 and you want to get 3 pears and they cost $1.2 each and you have$8.00. It should print 7.33333333333. Since you can’t buy 7.333 apples can you also figure out how to make it print just 7?

Calculate fundsAfterPears as funds - (pricePerPear * numPears). Calculate numApples as fundsAfterPears / pricePerApple. You can throw away the fractional part using int(num).

18. A car consumes fuel at a rate of 23 mpg. Someone fills the car up with 15 gallons of gas and drives 112 miles. Fill in the code to determine how many more gallons are left. The answer should be 10.13043478260869

Gas consumed is distance divided by gas rate and gas remaining is the amount of gas minus the gas consumed.

Create variables to hold each value. Calculate numGallons as tankCapacity * 0.25. Calculate numMiles as numGallons * milesPerGallon. Be sure to print the result.