pythonbeginner10 minutes

Refactor a Simple Temperature Converter Function

Improve the readability and efficiency of a basic Python function that converts temperatures from Celsius to Fahrenheit.

Challenge prompt

You are given a function that converts temperatures from Celsius to Fahrenheit, but the code is written in a messy way with unnecessary variables and redundant steps. Your task is to refactor the function to make it cleaner, simpler, and easier to understand, while keeping the same behavior and output.

Guidance

  • Remove any unnecessary intermediate variables to simplify the code.
  • Use meaningful variable names or streamline expressions directly.
  • Ensure the returned value is accurate and the function still works correctly.

Hints

  • Remember the formula: Fahrenheit = Celsius * 9/5 + 32.
  • You can return the expression directly without storing it in a separate variable first.
  • Avoid redundant prints or unused variables to keep the function concise.

Starter code

def celsius_to_fahrenheit(c):
    temperature_in_fahrenheit = 0
    temperature_in_fahrenheit = c * (9 / 5)
    temperature_in_fahrenheit = temperature_in_fahrenheit + 32
    return temperature_in_fahrenheit

Expected output

celsius_to_fahrenheit(0) # 32.0 celsius_to_fahrenheit(100) # 212.0 celsius_to_fahrenheit(-40) # -40.0

Core concepts

function refactoringbasic arithmetic operationsreturn statements

Challenge a Friend

Send this duel to someone else and see if they can solve it.