Add Comments and a Docstring
Beginner · Write the code · from Writing Clean Python: Comments, Docstrings, and PEP 8
From the lesson: Writing Clean Python: Comments, Docstrings, and PEP 8
Imagine opening a cookbook where every recipe is scribbled in messy handwriting, with no labels, no ingredient lists, and no spacing between steps. You could figure it out eventually, but it would be painful. Code works the same way.
Read the lessonWrite a function called celsius_to_fahrenheit that takes a temperature in Celsius and returns the Fahrenheit value.
The formula is: fahrenheit = celsius * 9/5 + 32
Your function must include:
Then print the result of converting 100 degrees Celsius. The output should be:
212.0# Write your function with a docstring and comment
What Check runs
Your code runs first, then each of these lines. The output must match exactly.
| Runs | Expected output |
|---|---|
| print(celsius_to_fahrenheit(100)) | 212.0 |
| print(celsius_to_fahrenheit(0)) | 32.0 |
| print(celsius_to_fahrenheit.__doc__ is not None) | True |
More Writing Clean Python exercises
- Fix the Bad Variable NamesBeginner
- Rewrite Messy Code with PEP 8 StyleBeginner
- Refactor: Clean Up This CodeBeginner
All Python exercises · 50 practice problems · Python Fundamentals quiz