Problem Set 8: Temperature Converter

When you start a new problem set, your first instinct might be to open your computer and begin typing code right away. While this can feel productive, it often leads to frustration when things don't work as expected. Instead, take a few minutes to slow down and plan.

Here are some helpful strategies:

  1. Understand the problem clearly
    • Read the instructions carefully — twice if needed.
    • Ask yourself: What exactly is being asked?
  2. Break the problem into smaller steps
    • Think about the smallest possible actions the computer will need to perform.
    • For example: If the task is to find the first recurring letter in a word, what steps must happen first?
  3. Try solving it on paper first
    • Write out your steps in plain language (pseudocode).
    • Test your steps with a simple example before touching the keyboard.
  4. Translate your steps into code
    • Start small — write only a few lines at a time and test often.
    • Don't worry about perfection at first; get a working version, then improve it.
  5. Check your solution
    • Run it with different examples, including edge cases.
    • Ask: Does this solve the problem in all situations?

  • Read the question carefully (twice).
  • Break the task into the smallest steps.
  • Sketch or write pseudocode before coding.
  • Start small — test as you go.
  • Check your solution with different cases.

Problem Description

Scientists, engineers, and everyday people often need to switch between different temperature scales:

  • Celsius (°C) is used in most of the world.
  • Fahrenheit (°F) is common in the United States.
  • Kelvin (K) is used in science (with no negative numbers).

Your task is to write a program that converts temperatures between Celsius, Fahrenheit, and Kelvin.


Learning Goals

By completing this problem, you should be able to:

  • Work with mathematical formulas in code.
  • Write and use functions to organize your program.
  • Apply user input and output.
  • Test your program with different values.

Conversion Formulas

You will need these formulas:

  • Celsius → Fahrenheit:
    F=95C+32F = \frac{9}{5}C + 32
  • Fahrenheit → Celsius:
    C=59(F32)C = \frac{5}{9}(F - 32)
  • Celsius → Kelvin:
    K=C+273.15K = C + 273.15
  • Kelvin → Celsius:
    C=K273.15C = K - 273.15
  • (You can combine formulas to go Fahrenheit → Kelvin or Kelvin → Fahrenheit.)

Tasks

Task 1: Convert Celsius → Fahrenheit

Write a program that:

  • Asks the user to enter a temperature in Celsius.
  • Converts it to Fahrenheit.
  • Prints the result.

Task 2: Convert Fahrenheit → Celsius

Expand your program:

  • Asks the user for a temperature in Fahrenheit.
  • Converts it to Celsius.
  • Prints the result.

Task 3: Add Kelvin

Update your program so it can handle all three scales:

  • Celsius ⇄ Fahrenheit
  • Celsius ⇄ Kelvin
  • Fahrenheit ⇄ Kelvin

(Hint: You don’t need to memorize new formulas—use Celsius as a “middle step.”)


Task 4: User Chooses Conversion

Modify your program to:

  • Ask the user which conversion they want (for example, "C to F", "F to C", "C to K", "K to F", etc.).
  • Perform the correct calculation.
  • Print the result clearly, including the unit.

 

Task 5: Test Cases

Test your program with the following inputs. Write down what your program prints:

  1. 0°C → Fahrenheit → Expected: 32°F
  2. 100°C → Fahrenheit → Expected: 212°F
  3. -40°C → Fahrenheit → Expected: -40°F
  4. 0 K → Celsius → Expected: -273.15°C
  5. 32°F → Celsius → Expected: 0°C
  6. 300 K → Fahrenheit → Expected: 80.33°F (approx.)

 

Extension Challenge (Optional)

Improve your program so that:

  • It keeps running in a loop until the user chooses to quit.
  • It rejects impossible values (for example, temperatures below 0 K).
  • It rounds results to 2 decimal places.

 

Success Criteria:

  • You can convert between Celsius, Fahrenheit, and Kelvin.
  • Your program handles multiple types of conversions correctly.
  • Your program works with the test cases provided.