Solution Design Think about how you can translate the mathematical equations in the Exercise Description into
Python code. For converting from Celsius to Fahrenheit, the ―Celsius‖ is the degreesCelsius
parameter, the × multiplication sign can be replaced by Python’s * operator, and the parentheses and
other math symbols have direct Python equivalents. You can think of the ―Fahrenheit =‖ and
―Celsius =‖ parts in the formula to Python return statements.
These functions can be one line long: a return statement that calculates the formula’s result and
returns it.
Special Cases and Gotchas When converting 42 degrees Celsius to Fahrenheit and back to Celsius in the Assertions section,
you may have noticed that you don’t get exactly 42 but 42.00000000000001. This tiny fractional part is
caused by rounding errors. Computers cannot perfectly represent all numbers with fractional parts.
For example, if you enter print(0.1 + 0.1 + 0.1) into Python’s interactive shell, it doesn’t
print 0.3 but rather prints 0.30000000000000004.
The Python programming language doesn’t cause these rounding errors. Instead, they come from
the technical standard for floating-point numbers used by all computers. Every programming
language has these same rounding errors. However, unless you are writing software for a bank or
nuclear reactor, these minor inaccuracies probably don’t matter. The Further Reading section
Now try to write a solution based on the information in the previous sections. If you still have
trouble solving this exercise, read the Solution Template section for additional hints.
Solution Template Try to first write a solution from scratch. But if you have difficulty, you can use the following
partial program as a starting place. Copy the following code from https://invpy.com/converttemp- template.py and paste it into your code editor. Replace the underscores with code to make a working
program:
def convertToFahrenheit(degreesCelsius):
# Calculate and return the degrees Fahrenheit: return ____ * (9 / 5) + 32
def convertToCelsius(degreesFahrenheit):
# Calculate and return the degrees Celsius: return (____ - 32) * (____ / ____)
The complete solution for this exercise is given in Appendix A and https://invpy.com/converttemp.py.
You can view each step of this program as it runs under a debugger at https://invpy.com/converttemp- debug/.