Temperature Converter
Mon Aug 22 2022 19:34:19 GMT+0000 (Coordinated Universal Time)
Saved by
@matteoarellano
#python
import re
print('Welcome to temperature converter. Please enter your input in this format: 40.0F, 35C, 0.50F etc.')
temperature = input("Please enter temperature in Farenheit or Celcius to convert:\n")
pattern = re.compile("^([+-]?[0-9]+(?:\.[0-9]*)?)\s*([CF])$")
found = re.findall(pattern, temperature)
if len(found) > 0:
temp = float(found[0][0])
conversion = found[0][1]
if conversion == 'C':
fahrenheit = (temp * 9 / 5) + 32
print(f"{temp: .2f} Celcius is equivalent to {fahrenheit: .2f} Farenheit")
elif conversion == 'F':
celcius = (temp - 32) * 5 / 9
print(f"{temp: .2f} Farenheit is equivalent to {celcius: .2f} Celcius")
else:
print(f"""Expecting a number followed by "C" or "F" \n I don't understand your input: {temperature}""")
content_copyCOPY
Comments