Answer to Question #228550 in Python for Varma

Question #228550

Temperature Conversion

You are given the temperature T of an object in one of Celsius, Fahrenheit, and Kelvin scales.

Write a program to print T in all scales viz Celsius, Fahrenheit, and Kelvin.

Formula to convert from Fahrenheit F to Celsius C is C = (F - 32) * 5 / 9.

Formula to convert from Kelvin K to Celsius C is C = K - 273.

Here "C", "F", "K" represent that the temperature scale is in Celsius, Fahrenheit and Kelvin scales respectively.

The input contains the temperature (a number) and the unit of the temperature scale (C, F, K) without any space.

The output contains temperature in Celsius, Fahrenheit and Kelvin scales in each line in the format similar to input and the value of the temperature is rounded to 2 decimal places.


1
Expert's answer
2021-08-23T01:19:41-0400
# Python 3.9.5

def enter_temperature():
    temp = input("Enter the temperature : ")
    return temp

def get_conversion_temperature(temp):

    # if temperature is in kelvin
    if temp[-1] == "k" or temp[-1] == "K":
        x = int(temp[:-1])
        c = x - 273.15
        k = x
        f = (9 * c / 5) + 32

    # if temperature is in celcius
    elif temp[-1] == "c" or temp[-1] == "C":
        x = int(temp[:-1])
        c = x
        k = x + 273.15
        f = (9 * c / 5) + 32

    # if temperature is in fahrenheit
    elif temp[-1] == "f" or temp[-1] == "F":
        x = int(temp[:-1])
        c = (x - 32) * 5 / 9
        k = c + 273.15
        f = x

    print("Temperature in Celcius is :", round(c, 2) , " C")
    print("Temperature in Kelvin is :", round(k, 2) , " K")
    print("Temperature in Fahrenheit is :", round(f, 2) , " F")

def main():
    temp = enter_temperature()
    get_conversion_temperature(temp)

if __name__ == '__main__':
   main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS