In: Computer Science
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message. For example, if the user enters F and 10, it means that the temperature is 10 degrees in Fahrenheit, so the program will print:10 degrees Fahrenheit is equal to -12.222 degrees in Celcius.
IMPORTANT NOTE: Your program should keep on asking temperature and do the conversion until the user enters 1000 which is assumed to be the sentinel value.
Required Program in Python -->
def CelsiustoFahrenheit(c):
f = (9/5)*c + 32
return f
def FahrenheittoCelsius(f):
c = (5/9)*(f - 32)
return c
if __name__ == "__main__" :
choice=input("Which unit you want to convert ?")
while(1):
if(choice=='c' or choice=='C'):
c= int(input("Enter temp in C"))
if(c==1000):
break
print(c, "degree celsius is equal
to:",CelsiustoFahrenheit(c),"Fahrenheit")
elif(choice=='f' or choice=='F'):
f= int(input("Enter temp in F"))
if(f==1000):
break
print(f,"Fahrenheit is equal to:",FahrenheittoCelsius(f),"degree
celsius")
CelsiustoFahrenheit() and FahrenheittoCelsius() are two functions called to find out the conversion from c to f or vice versa. First, user is asked for the input, then an infinite loop runs till a break occurs, break occurs when the value input by the user is 1000. User is asked for a value, that value is then given to a function, which returns the converted value and prints the o/p on screen.