SoFunction
Updated on 2024-11-18

Example method for converting temperature units in python

Temperature has two units, Celsius and Fahrenheit, we usually use Celsius, for converting to Fahrenheit, many partners can not remember the formula. As a universal computer, it is possible to help us solve the problem of temperature unit conversion. This article mainly demonstrates the code process of temperature unit conversion in python, please see this article.

I. Issues

Temperature has two different units, Celsius and Fabrenheit. Celsius is 0 degrees for the freezing point and 100 degrees for the boiling point; Fahrenheit is 32 degrees for the freezing point and 212 degrees for the boiling point. Generally, China uses Celsius and the United States uses Fahrenheit.

The formula for converting between the two is: Celsius = (Fahrenheit - 32)/1.8, Fahrenheit = Celsius * 1.8 + 32.

II. Codes

importation

# Define a function to get a signed temperature value.
def tempstr():
  while True:
    temp=input('Please enter the temperature value with the symbol [C for Celsius, F for Fahrenheit]:')
    if temp[-1] in ['c','C','f','F']:
      return temp
    else: # If a temperature value is entered without a symbol, an input error will be indicated and you will be asked to re-enter it.
      print('Input error, please enter a temperature value with a symbol')
      print('-'*20)

process output

# Define a function to get a signed temperature value.
def tempstr():
  while True:
    temp=input('Please enter the temperature value with the symbol [C for Celsius, F for Fahrenheit]:')
    if temp[-1] in ['c','C','f','F']:
      return temp
    else: # If a temperature value is entered without a symbol, an input error will be indicated and you will be asked to re-enter it.
      print('Input error, please enter a temperature value with a symbol')
      print('-'*20)

Master Code

def tempstr():
  while True:
    temp=input('Please enter the temperature value with the symbol [C for Celsius, F for Fahrenheit]:')
    if temp[-1] in ['c','C','f','F']:
      return temp
    else: 
      print('Input error, please enter a temperature value with a symbol')
      print('-'*20)
def progress(temp):
  if temp[-1] in ['F','f']:
    output=(eval(temp[:-1])-32)/1.8
    print('The temperature is converted to Celsius as{:.2f}C'.format(output))
  else:
    output=eval(temp[:-1])*1.8+32
    print('The temperature conversion to Fahrenheit is{:.2f}F'.format(output))
temp=tempstr()
progress(temp)

Temperature unit conversion example extension:

module:temp

def temp_f_to_c(f):
  return (f - 32) * (5 / 9)
def temp_c_to_f(c):
  return (c * 9 / 5) + 32
def main():
  print(temp_c_to_f(100))
if __name__ == '__main__':
  main()

main function:

import temps
 
def convert_temp_system(temp, temp_system):
  if temp_system == 'c':
    new_temp = temps.temp_c_to_f(temp)
  else:
    new_temp = temps.temp_f_to_c(temp)
 
  return new_temp
 
 
def print_temp_message(original_temp, new_temp, system):
  if system == 'f':
    print(original_temp, 'degrees F converted to C is ', new_temp)
  else:
    print(original_temp, 'degrees C converted to F is ', new_temp)
 
 
def main():
  temp = float(input('Enter the temperature: '))
  system = input('F or C: ')
  converted_temp = convert_temp_system(temp, system)
  print_temp_message(temp, converted_temp, system)
 
if __name__ == '__main__':
  main()

to this article on the python temperature unit conversion example method of the article is introduced to this, more related python temperature units how to convert the contents of the search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!