Python Exercises - How to convert Celsius to Fahrenheit | Python exercise #6

Pybeginner
By -
0

This is a simple Python program where you can convert the temperature which is in celsius to fahrenheit. 


In order to do this exercise, first we need to get the celsius from the user. As usual as we did in the previous exercise we will use the eval method to get the desired variable type.


Now it’s time to convert the celsius to fahrenheit.

Tips to follow :

  1. Take the input of celsius from the user.

  2. Convert the celsius to fahrenheit using the formula - fahrenheit = 9/5 * celsius + 32.

  3. If it is greater than 90, print the result as really hot.

  4. If it is less than 30, print the result as extremely cold.


Video explanation :




Code


#      A program to convert Celsius temps to Fahrenheit.
#      This version issues heat and cold warnings.
 
celsius = eval(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is", fahrenheit, "degrees Fahrenheit.")
 
# Print warnings for extreme temps
if fahrenheit > 90:
    print("It's really hot out there. Be careful!")
if fahrenheit < 30:
    print("Brrrrr. Be sure to dress warmly!")



Program to convert Celsius temperature to Fahrenheit using Functions :


#      A program to convert Celsius temps to Fahrenheit using functions.
#      This version issues heat and cold warnings.
 
def convertor():
    celsius = eval(input("What is the Celsius temperature? "))
    fahrenheit = 9/5 * celsius + 32
    print("The temperature is", fahrenheit, "degrees Fahrenheit.")
 
    # Print warnings for extreme temps
    if fahrenheit > 90:
        print("It's really hot out there. Be careful!")
    if fahrenheit < 30:
        print("Brrrrr. Be sure to dress warmly!")
 
convertor()



Post a Comment

0Comments

Post a Comment (0)