4. Celsius to Fahrenheit
Write a function to_fahrenheit(celsius) that takes a temperature in degrees Celsius and returns the same
temperature in degrees Fahrenheit.
The formula is: multiply by 9, divide by 5, then add 32. Dividing with / always gives a decimal number (a float),
so the answer is a float such as 212.0, even when the temperature is a whole number.
Example 1
celsius = 100212.0Example 2
celsius = 032.0Example 3
celsius = -40-40.0Constraints
celsiusis a whole number or a decimal from -273.15 to 1000. It can be negative.- Decimal arithmetic can be a tiny bit off: -273.15 gives
-459.66999999999996, not-459.67. That's fine: an answer within 0.000000001 of the expected one counts as right.
Submit also runs 4 more tests you can't see.
Practises Numbers and Math (lesson 3).
- Test your formula on two temperatures you know: water freezes at 0 °C, which is 32 °F, and boils at 100 °C, which is 212 °F.
- In Python the formula is
celsius * 9 / 5 + 32.*and/happen before+, so it needs no brackets, and/always gives a float. - Don't round the answer: -273.15 gives
-459.66999999999996, and the tests accept that tiny difference.
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
def to_fahrenheit(celsius):
"""Convert a temperature from Celsius to Fahrenheit.
celsius: int or float, the temperature in degrees C
Returns: float, the same temperature in degrees F
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.