Answer to Question #188628 in Python for MARTHALA SUNILREDD

Question #188628

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-05-03T17:26:04-0400

Here is the code:

def parse_input(user_input: str) -> dict:
   result = {'value': None, 'scale': None}
   scale = user_input[-1]
   if scale in 'KCF':
       result['scale'] = scale
       value = float(user_input[0:-1])
       result['value'] = value
   else:
       print('Wrong input!')
       exit(0)

   return result

def fahrenheit_celsium_conversion(value: float, direction='forward') -> float:
   result = None
   if direction == 'forward':
       result = (value - 32) * 5 / 9
   elif direction == 'backward':
       result = (value * 9 / 5) + 32
   else:
       print('Internal error!')
       exit(0)

   return result

def kelvin_celsium_conversion(value: float, direction='forward') -> float:
   result = None
   if direction == 'forward':
       result = value - 273
   elif direction == 'backward':
       result = value + 273
   else:
       print('Internal error!')
       exit(0)

   return result

def kelvin_fahrenheit_conversion(value: float, direction='forward') -> float:
   result = None
   if direction == 'forward':
       result = (value - 273.15) * 9 / 5 + 32
   elif direction == 'backward':
       result = (value - 32) * 5 / 9 + 273.15
   else:
       print('Internal error')
       exit(0)

   return result

def print_data(value: float, scale: str) -> None:
   print("%.2f" % value, scale, sep='')

if __name__ == '__main__':
   user_input = input('Enter temperature: ')
   data = parse_input(user_input)
   if data['scale'] == 'K':
       print_data(kelvin_celsium_conversion(data['value']), 'C')
       print_data(kelvin_fahrenheit_conversion(data['value']), 'F')
       print_data(data['value'], 'K')
   elif data['scale'] == 'C':
       print_data(data['value'], 'C')
       print_data(fahrenheit_celsium_conversion(data['value'], 
           direction='backward'), 'F')
       print_data(kelvin_celsium_conversion(data['value'], 
           direction='backward'), 'K')
   elif data['scale'] == 'F':
       print_data(fahrenheit_celsium_conversion(data['value']), 'C')
       print_data(data['value'], 'F')
       print_data(kelvin_fahrenheit_conversion(data['value'],
           direction='backward'), 'K')
   else:
       print('Internal error!')

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