Skip to main content

Exercises 2.18 Exercises

1.

Evaluate the following numerical expressions in your head, then use the active code window to check your results:
  1. 5 ** 2
  2. 9 * 5
  3. 15 / 12
  4. 12 / 15
  5. 15 // 12
  6. 12 // 15
  7. 5 % 2
  8. 9 % 5
  9. 15 % 12
  10. 12 % 15
  11. 6 % 6
  12. 0 % 7
Solution.
  1. 5 ** 2 = 25
  2. 9 * 5 = 45
  3. 15 / 12 = 1.25
  4. 12 / 15 = 0.8
  5. 15 // 12 = 1
  6. 12 // 15 = 0
  7. 5 % 2 = 1
  8. 9 % 5 = 4
  9. 15 % 12 = 3
  10. 12 % 15 = 12
  11. 6 % 6 = 0
  12. 0 % 7 = 0

2.

What is the order of the arithmetic operations in the following expression. Evaluate the expression by hand and then check your work.

3.

Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will go off at hour 15 (3pm).
Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on the clock when the alarm goes off.
Solution.
## question 3 solution ##

current_time_string = input("What is the current time (in hours)? ")
waiting_time_string = input("How many hours do you have to wait? ")

current_time_int = int(current_time_string)
waiting_time_int = int(waiting_time_string)

hours = current_time_int + waiting_time_int

timeofday = hours % 24

print(timeofday)

4.

It is possible to name the days 0 through 6 where day 0 is Sunday and day 6 is Saturday. If you go on a wonderful holiday leaving on day number 3 (a Wednesday) and you return home after 10 nights you would return home on a Saturday (day 6) Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the number of day of the week you will return on.

5.

Add parentheses to the expression 6 * 1 - 2 to change its value from 4 to -6.

6.

The formula for computing the final amount if one is earning compound interest is given on Wikipedia as:
\begin{equation*} A = {P \left (1 + {r \over n} \right )^{nt}} \end{equation*}
where:
  • P is the principal amount (initial investment)
  • r is the annual nominal interest rate (as a decimal)
  • n is the number of times the interest is compounded per year
  • t is the number of years
Write a Python program that assigns the principal amount of 10000 to variable P, assign to n the value 12, and assign to r the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, t, that the money will be compounded for. Calculate and print the final amount after t years.
Solution.
## compound interest solution ##

P = 10000
n = 12
r = 0.08

t = int(input("Compound for how many years? "))

final = P * ( ((1 + (r/n)) ** (n * t)) )

print ("The final amount after", t, "years is", final)

7.

Write a program that will compute the area of a circle. Prompt the user to enter the radius and print a nice message back to the user with the answer.

8.

Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. The values can have decimal places (for example, a width of 7.25 and a height of 4.9). Print a nice message with the answer.
Solution.
## rectangle area solution

width = float(input("Width? "))
height = float(input("Height? "))

area = width * height

print("The area of the rectangle is", area)

9.

Write a program that will compute MPG (miles per gallon) for a car. Prompt the user to enter the number of miles driven and the number of gallons used. Print a nice message with the answer.

10.

Write a program that will ask the user for a temperature in degrees Celsius and convert it to degrees Fahrenheit. The conversion formula is \(C \times {9 \over 5} + 32\text{,}\) where C represents the temperature in degrees Celsius. The Celsius temperature can have decimal places (for example, 17.3 degrees Celsius)
Solution.
## Temperature conversion solution ##

deg_c = float(input("What is the temperature in Celsius? "))

# formula to convert C to F is: (degrees Celsius) times (9/5) plus (32)
deg_f = deg_c * (9 / 5) + 32

print(deg_c, " degrees Celsius is", deg_f, " degrees Farenheit.")

11.

Write a program that will ask the user for a temperature in degrees Fahrenheit and convert it to degrees Celsius. The conversion formula is \((F - 32) \times {5 \over 9}\text{,}\) where F represents the temperature in degrees Fahrenheit. The Fahrenheit temperature can have decimal places (for example, 57.3 degrees Fahrenheit)