Create A Temperature Converter in Python- part 2|Python Beginner Project
This is part 2 of the Project Problem in Python to Create a Temperature Converter and in this part, we will learn how to solve the problem and build this Project
Introduction
In part 1 of this tutorial, we had learnt how to understand the project guide and what steps to take in order to set out to build this project and create a temperature converter in Python. In this part 2, we will be doing the actual solving part with source code. Were you able to build this project before reading this solution? :-)
Defining the Temperature Converter
We need to define a function which we are using for the temperature conversion from celsius to fahrenheit and vice versa. We use the def keyword to define this function and we call it cels_fah. By the way, we can name it anyways, for instance, celsius_fah or celsius_fahrenheit etc etc.
Inside this function we will carry out some tasks such as returning the value of the conversion from celsius to fahrenheit using the standard formula for such conversion calculations : celsius * 9/5) + 32
Next we define another function namely fah_cels with the parameter of fahrenheit since it is accepting fahrenheit value to be converted to celsius .We repeat what we did in the function above .
In this function, we do the task of returning the value of conversion from fahrenheit to celsius by using the standard formula: fahrenheit -32) * 5/9
Code:
def cels_fah(celsius):
return(celsius * 9/5) + 32)
def fah_cels(fahrenheit ):
return(fahrenheit -32) * 5/9)
Creating the While True Loop
In the while True loop, firstly we create the different choices or options that are offered to the user in order for them to enter in the beginning of using the converter. We allocate option 1 for conversion from celsius to fahrenheit and option 2 from fahrenheit to celsius and option 3 for exit and end the conversion.
Code
while True:
option = int(input("Enter your option > \n"
"1. celsius to fahrenheit \n"
"2. fahrenheit to celsius \n"
"3. exit \n"
))
Creating the if-elif-else Loop
We need to create this conditional loop in order to determine what action the user will take depending on the options that he chooses. If the user enters the option of 1, he will be asked to enter the temperature in celsius. For this reason,we need to use the keyword input to prompt the user to enter the option and also use the float keyword to accept the values in numbers that could be decimals as well since temperature readings could be decimal numbers as well.
Finally, the programme also prints out the result of the conversions respectively for the user to take down for his/her use. If the user does not select the options 1 or 2 , but selects option 3, then we call break and the user is prompted to quit the program.
Code
if option == 1:
celsius = float(input("Enter the temperature in celsius"))
print("The temperature in fahrenheit is", cels_fah(celsius))
elif option == 2:
fahrenheit = float(input("Enter the temperature in fahrenheit"))
print(" The temperature in celsius is", fah_cels(fahrenheit))
else:
print("quit")
break
Full Source Code
def cels_fah(celsius):
return(celsius * 9/5) + 32)
def fah_cels(fahrenheit ):
return(fahrenheit -32) * 5/9)
while True:
option = int(input("Enter your option > \n"
"1. celsius to fahrenheit \n"
"2. fahrenheit to celsius \n"
"3. exit \n"
))
if option == 1:
celsius = float(input("Enter the temperature in celsius"))
print("The temperature in fahrenheit is", cels_fah(celsius))
elif option == 2:
fahrenheit = float(input("Enter the temperature in fahrenheit"))
print(" The temperature in celsius is", fah_cels(fahrenheit))
else:
print("quit")
break
Conclusion
We learnt how to build a Temperature Converter using Python3 in few lines of code. If you have any doubts that you can not follow in the code, do mention in the comment section below.