121k views
4 votes
Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning function defined to take a Fahrenheit temperature as a parameter. This function should calculate the equivalent Celsius temperature and return it. In the main function, your program should:

1 Answer

1 vote

A Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions.

Step-by-step explanation:

  • Functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter.
  • It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places.
  • Custom function f_to_c should be a value-returning function defined to take a Fahrenheit temperature as a parameter.
  • This function should calculate the equivalent Celsius temperature and return it.

The code is given below :

def c_to_f(tempCelsius):

tempFahrenheit = ((9/5)*tempCelsius) + 32;

print("\\ %.3f Celsius is %.3f Fahrenheit \\" %(tempCelsius, tempFahrenheit));

def f_to_c(tempFahrenheit):

tempCelsius = (tempFahrenheit - 32) * (5/9);

return tempCelsius;

(Import modules)

import temps;

def main():

temperature = float(input("\\ Enter a temperature: "));

scale = input("\\ Was that input Fahrenheit or Celsius c/f? ");

if scale.lower() == 'c':

temps.c_to_f(temperature);

else:

tempCel = temps.f_to_c(temperature);

print("\\ %.1f Fahrenheit equals %.3f Celsius \\" %(temperature, tempCel));

main();

User GNassro
by
5.9k points