Write a function for temperature conversion named ‘convert_temp’.
- It should be able to handle both Fahrenheit to Celsius conversions as well as Celsius to Fahrenheit conversions.
- It must accept and read two arguments that are passed to it: first, the temperature scale of the original temperature (only ‘F’ or ‘C’ should be used) and second, the number of degrees of the original temperature.
- It should then convert the original temperature from Fahrenheit to Celsius or from Celsius to Fahrenheit, as appropriate.
- Finally, this function should print out the original temperature and scale as well as the converted temperature and scale.
The function signature should be:
def convert_temp( scale=None, source_temp=None ):
Then write a short program to use the function you wrote (both the function and program should be part of the same file). Your program should prompt the user to enter a temperature scale (assume the user will choose to type ‘F’ or ‘C’) and then prompt the user to enter a number of degrees. Using the values supplied by the user, the program should then call the ‘convert_temp’ function and pass in the two arguments, along these lines:
convert_temp(scale=F, source_temp=98.6)
and the subroutine should produce a line of output that looks like this (for Fahrenheit to Celsius):
98.6 degrees F is 37.0 degrees C
or, for a Celsius to Fahrenheit conversion:
100.0 degrees C is 212.0 degrees F
The first temperature and scale that you should report are those that the user entered, followed by the converted temperature and other scale.
I have the following code so far:
#!/usr/bin/env python3
def convert_temp(scale=None, source_temp=None):
if scale == "F":
return(source_temp - 32.0) * (5.0/9.0)
elif scale == "C":
return(source_temp * (9.0/5.0)) + 32.0
else:
print("Needs to be (F) or (C)!")
scale = input("Select (F) or (C): " )
source_temp = int(input("What is the temperature: " ))
m = convert_temp(scale, source_temp)
print(source_temp, "degrees", scale, "is", m, "degrees", scale)
What need help with, is to add the converted scale (F or C) to my print output.