Converting Celsius to Fahrenheit in Python

Question:

I’m new to Python and stuck on this code to convert Celsius to Fahrenheit. I have to use sys.argv and print out the corresponding Fahrenheit value to two decimals. Although, it does not give me an error, the answer is incorrect because the output is always 1 decimal.

Here’s my code:

import sys
def TempCtoF(Celsius):
    Celsius = float(Celsius)
    Fahrenheit = float((9/5 * Celsius)+ 32)
    return Fahrenheit
Celsius = sys.argv[1]
Fahrenheit = TempCtoF(Celsius)
print("The temperature is", round(Fahrenheit, 2),"degrees Fahrenheit.")
Expected output:
The temperature is 73.40 degrees Fahrenheit.

Actual output:
The temperature is 73.4 degrees Fahrenheit.

However, it seems to be correct when the temperature is in the 3 digits.

Expected output:
The temperature is 132.03 degrees Fahrenheit.

Actual output:
The temperature is 132.03 degrees Fahrenheit.

I am not quite sure how to fix this. Please help!

Asked By: MCly

||

Answers:

You can simply replace the print statement with this –

print(f"The temperature is {round(Fahrenheit, 2):.2f} degrees Fahrenheit.")
Answered By: Zero
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.