3.10. Chapter 3 Exercises¶
Write down what you think each of these would print, then use the active code window to check your results:
9 * 5
2 / 5
5 % 2
9 % 5
6 % 6
2 % 7
3 / 0
Insert the correct operators in place of the
#s so each line prints
==checks for equality.
Add a set of parentheses to the expression
x = 6 * 1 - 2so that the code below prints -6 instead of 4.
Add parentheses to
x = 12 * 2 - 3 + 4 * 2so that it prints -4 instead of 29.
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.
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)
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.
Fix the syntax errors.
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.
Fix the syntax and semantic errors so that the answer is 1 instead of 3.5
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.
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)
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.
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
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.
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
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?
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
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.
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)