Prev - #1 Hello, World! | Table of Contents | Next - #3 Odd & Even
Exercise #2: Temperature Conversion convertToCelsius(32) → 0.0
convertToFahrenheit(100) → 212
Converting between Celsius and Fahrenheit involves a basic calculation and provides a good exercise for writing functions that take in a numeric input and return a numeric output. This exercise tests your ability to use Python’s math operators and translate math equations into Python code.
Exercise Description
Write a convertToFahrenheit()
function with a degreesCelsius
parameter. This function returns the number of this temperature in degrees Fahrenheit. Then write a function named convertToCelsius()
with a degreesFahrenheit parameter and returns a number of this temperature in degrees Celsius.
Use these two formulas for converting between Celsius and Fahrenheit:
· Fahrenheit = Celsius × (9 / 5) + 32
· Celsius = (Fahrenheit - 32) × (5 / 9)
These Python assert
statements stop the program if their condition is False
. Copy them to the bottom of your solution program. Your solution is correct if the following assert
statements’ conditions are all True:
assert convertToCelsius(0) == -17.77777777777778
assert convertToCelsius(180) == 82.22222222222223
assert convertToFahrenheit(0) == 32
assert convertToFahrenheit(100) == 212
assert convertToCelsius(convertToFahrenheit(15)) == 15
assert convertToCelsius(convertToFahrenheit(42)) == 42.00000000000001
Try to write a solution based on the information in this description. If you still have trouble solving this exercise, read the Solution Design and Special Cases and Gotchas sections for additional hints.
Prerequisite concepts: math operators
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):
return ____ * (9 / 5) + 32
def convertToCelsius(degreesFahrenheit):
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/.
Further Reading
You can use Python’s decimal
module to represent fractional numbers more accurately than floating-point values can. You can learn about this module at the Python Module of the Week blog at https://pymotw.com/3/decimal/.
One programming technique to avoid rounding errors is to stick with integers: instead of representing $1.25 as the float 1.25
you can use the integer 125
for 125 cents, or instead of representing 1.5 minutes as the float 1.5
you can use the integer 90
for 90 seconds or even 90000 for 90,000 milliseconds. Integers and the decimal module’s Decimal
objects don’t suffer from rounding errors like floating-point numbers do.
Prev - #1 Hello, World! | Table of Contents | Next - #3 Odd & Even
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4