Skip to main content
Practice

Coding Quiz - Python Temperature Converter

This coding quiz involves writing a function to convert temperatures between Celsius(C) and Fahrenheit(F).

The solution function should take a temperature in Celsius and convert it to Fahrenheit, or take a temperature in Fahrenheit and convert it to Celsius.

Function Signature
def solution(temperature, scale):
# Write your code here
return # Write your return value here
  • temperature is the parameter representing the temperature you want to convert.

  • scale takes one of two values: C (Celsius) or F (Fahrenheit).

  • Use conditional statements to write code that converts Celsius to Fahrenheit when scale is C and Fahrenheit to Celsius when scale is F.


Constraints

  • The input temperature should be either an integer or a float.

  • Use the following formulas to convert between Celsius and Fahrenheit:

    • From Celsius to Fahrenheit: (Celsius temperature × 9/5) + 32
    • From Fahrenheit to Celsius: (Fahrenheit temperature - 32) × 5/9

Example Input and Output

  • Input: Celsius 25, Output: Fahrenheit 77

  • Input: Fahrenheit 77, Output: Celsius 25

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.