Sure, here is a simple temperature conversion program, as requested:

def celsius_to_fahrenheit(celsius):
  """Converts Celsius to Fahrenheit."""
  return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
  """Converts Fahrenheit to Celsius."""
  return (fahrenheit - 32) * 5/9

# Get input from the user
while True:
  try:
    value = float(input("Enter temperature value: "))
    break
  except ValueError:
    print("Invalid input. Please enter a number.")

while True:
  unit = input("Enter unit (C for Celsius, F for Fahrenheit): ").upper()
  if unit in ['C', 'F']:
    break
  else:
    print("Invalid unit. Please enter 'C' or 'F'.")

# Perform conversion and print the result
if unit == 'C':
  converted_value = celsius_to_fahrenheit(value)
  print(f"{value}°C is equal to {converted_value:.2f}°F")
else:
  converted_value = fahrenheit_to_celsius(value)
  print(f"{value}°F is equal to {converted_value:.2f}°C")

How to run the code:

  1. Save the code as a Python file (e.g., temperature_converter.py ).
  2. Open a terminal or command prompt.
  3. Navigate to the directory where you saved the file.
  4. Run the script using the command: python temperature_converter.py

The program will then prompt you to enter a temperature value and its unit, and it will display the converted temperature.


Let me know if you have any other requests or would like to explore more advanced features for this program!

Other Articles: