99.7k views
4 votes
Write a program that repeatedly asks the user for a scale f or a c (for "fahrenheit" or "celsius") on one line followed by an integer temperature on the next line. it then converts the given temperature to the other scale. use the formulas: mips

1 Answer

3 votes
You should really specify what language you're using when you're asking a programming related question. I did modify the input slightly, to use xc or xf, where x is the given temperature.
Here it is in Python:

import re

def to_celsius(fahrenheit):
return (fahrenheit - 32) / 1.8

def to_fahrenheit(celcius):
return celcius * 1.8 + 32

while True:
_in = input().lower()
if re.match(r'\d+(.\d+)?c', _in):
print(f'{_in} is {to_fahrenheit(float(_in.replace("c", "")))}f')
elif re.match(r'\d+(.\d+)?f', _in):
print(f'{_in} is {to_celsius(float(_in.replace("f", "")))}c')
else:
print('Invalid input.')

User Greg Trevellick
by
6.7k points