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}""")
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter