0
votes

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.

3
Hi and welcome. It looks like you have made progress. It's not that clear what you are specifically stuck with? - wwkudu

3 Answers

1
votes

The easiest thing you can do is to have the function return the scale of the converted temperature, since it already bases its behavior on the given scale.

It can simply return a tuple, with the destination scale as its first element

#!/usr/bin/env python3
def convert_temp(scale=None, source_temp=None):
    if scale == "F":
        return 'C', (source_temp - 32.0) * (5.0/9.0)
    elif scale == "C":
        return 'F', (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: " ))
s, m = convert_temp(scale, source_temp)
print(source_temp, "degrees", scale, "is", m, "degrees", s)

so now the output shows both the original scale and the converted one:

Select (F) or (C): F
What is the temperature: 92
92 degrees F is 33.333333333333336 degrees C

Select (F) or (C): C 
What is the temperature: 33
33 degrees C is 91.4 degrees F

Note: the function prints out an error message if the scale given is not supported. It should raise an exception instead.

Note2: why not convert the input numbers to float?

0
votes

You need to indent after the :...

#!/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)!")
        return

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)
0
votes

Just working off the answers above, I gave it a tweak for the benefit of a lazy user - who may not capitalize the F or C at the input part... and instead put "f" or "c" in lower case... this answer fixes that, and returns scale correctly formatted in title case in the print statement.

def convert_temp(scale=None, source_temp=None):
if (scale == "F") or (scale == "f"):
    return 'C', (source_temp - 32.0) * (5.0/9.0)
elif (scale == "C") or (scale == "c"):
    return 'F', (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: " ))
s, m = convert_temp(scale, source_temp)
print(source_temp, "degrees", scale.title(), "is", m, "degrees", s)