CBSE Class 11 Computer Science Question 41 of 43

Flow of Control — Question 36

Back to all questions
36
Question

Question 28

Write a Python script to input temperature. Then ask them what units, Celsius or Fahrenheit, the temperature is in. Your program should convert the temperature to the other unit. The conversions are:

F = 9/5C + 32 and C = 5/9 (F 32).

Solution
temp = float(input("Enter Temperature: "))
unit = input("Enter unit('C' for Celsius or 'F' for Fahrenheit): ")

if unit == 'C' or unit == 'c' :
    newTemp = 9 / 5 * temp + 32
    print("Temperature in Fahrenheit =", newTemp)
elif unit == 'F' or unit == 'f' :
    newTemp = 5 / 9 * (temp - 32)
    print("Temperature in Celsius =", newTemp)
else :
    print("Unknown unit", unit)
Output
Enter Temperature: 38
Enter unit('C' for Celsius or 'F' for Fahrenheit): C
Temperature in Fahrenheit = 100.4
Answer

temp
=
float
(
input
(
"Enter Temperature: "
))
unit
=
input
(
"Enter unit('C' for Celsius or 'F' for Fahrenheit): "
)
if
unit
==
'C'
or
unit
==
'c'
:
newTemp
=
9
/
5
*
temp
+
32
print
(
"Temperature in Fahrenheit ="
,
newTemp
)
elif
unit
==
'F'
or
unit
==
'f'
:
newTemp
=
5
/
9
*
(
temp
-
32
)
print
(
"Temperature in Celsius ="
,
newTemp
)
else
:
print
(
"Unknown unit"
,
unit
)
Output
Enter Temperature: 38
Enter unit('C' for Celsius or 'F' for Fahrenheit): C
Temperature in Fahrenheit = 100.4