# 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
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.

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

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.

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.

8. Fix the syntax errors.

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. Show Comments 10. Fix the syntax and semantic errors so that the answer is 1 instead of 3.5 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. 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) 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. 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

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.

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

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?

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

19. Write the code to calculate and print how many miles you can drive if your car holds 10 gallons and you have a quarter of a tank left and your car gets 32 miles per gallon. It should print 80.

20. A bullet is travelling 25 m/s. Write code to determine how many seconds it will take to travel 111 m. (It should be 4.44 seconds)