Conversion of temperature from and to

Write a Python program to convert temperatures to and from celsius, fahrenheit.

c = (5/9) * (F-32);

f = (9/5)*C (+32);

temp = input('Enter the temperature you wants to convert : ')
degree = int(temp[:-1])
i_convention = temp[-1]

if i_convention.upper() == 'C':
    result = int(round((9*degree)/5 + 32))
    o_convention = "Fahrenheit"
elif i_convention.upper() == "F":
    result = int(round((degree-32)*5/9))
    o_convention = "Celsius"
else:
print('Enter the temperature with proper convention; F / C'
quit()
print("The temperature in ", o_convention, "is", result, "degrees")

NameError: name ‘o_convention’ is not defined. Is it necessary to define the ‘o_convention’ ?

o_convention is defined within the if-elif. However if for whatever reason i_convention is neither c/C nor f/F, the if-elif won’t be executed and hence o_convention will be undefined.

So that could throw the error.

1 Like

I found out the error. While inputting the temperature we should clearly mention whether it is in Fahrenheit or in Celsius. I made a small change in my code. Now it is working very well.
Thank you for your suggestions.