Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Display Converted Temperatures

00:00 With both functions in place, you can tackle the next part of the challenge, which is prompting the user to enter a temperature and then convert Fahrenheit to Celsius and Celsius to Fahrenheit and then display all converted temperatures rounded to two decimal places.

00:19 Now this is very interesting how you want to tackle it. I decide to postpone the prompting part for later because otherwise when you want to test if your code works, you will be prompted all the time.

00:32 So instead of tackling the prompting part, I first tackle the displaying part. For this, I create a variable temp_far, which I set to 72.

00:49 This would be something that later we can prompt the user for. So it’s a nice idea to add a to-do there so we don’t forget to prompt the user later. And a temp_cel variable, which is the converted value from convert_far_to_cel().

01:05 So you pass in temp_far there, and then you can print temp_cel.

01:15 Okay. With this code in place, you can save and run it. And as you can see, there is the 22.222 and so on. So generally, the code works as expected.

01:27 There are a few adjustments that you need to do in a moment, but for now, continue with the temp_cel variable that you now set to the integer 37.

01:40 Again, this will be something that later, this will be an input from the user

01:46 and then temp_far is convert_cel_to_far() with temp_cel as the argument.

01:56 And then again, make a print() function call with temp_far as the argument. And then when you save and run it, you have the 98.6 with a bunch of zeros and the one as a result.

02:11 That means the first two parts of this part of the challenge are solved. And next, we should tackle the displaying of the decimal places behind the dot.

Avatar image for John Akpan

John Akpan on July 7, 2024

I did try a different approach and it works

def convert_cel_to_far(temp_cel):
    temp_far = float(temp_cel) * ( 9 / 5 ) + 32
    return round(temp_far, 2)



def convert_far_to_cel (temp_far):
    temp_cel = float(temp_far - 32 ) * 5 / 9
    return round(temp_cel, 2)


conversion_type = input("what is the conversion type: ? \nEnter 'A' if celcius to fahrenheit\nEnter 'B' if fahrenheit to celcius\n: ")
conversion_type = conversion_type.lower()


if conversion_type == "a":
    temp_cel = (input("Enter your temperature in celcius: "))
    try:
        temp_far = convert_cel_to_far(temp_cel)
        print(f"The temperature in celcius {temp_cel} is converted to {temp_far} in Farenheit")
    except ValueError:
        print("Please enter a valid numeric value for the temperature.")


elif conversion_type == "b":
    temp_far = (input("Enter your temperature in farenheit: "))
    try:
        temp_cel = convert_far_to_cel(temp_far)
        print(f"The temperature in celcius {temp_far} is converted to {temp_cel} in Celcius")
    except ValueError:
        print("Please enter a valid numeric value for the temperature.")

Become a Member to join the conversation.