Doubt in the one part of If Else Case Video

In the Temperature project video, why do we have to add this line to the code:
temp=int(temp)
I did not understand this completely, please somebody explain in detail.

this is to typecast (change the type ) ‘temp’ from ‘string’ to ‘int’.
You must have defined temp as:
temp = ‘35’

This tells python that ‘35’ is a string and NOT a number.

to directly input a number :
temp = 35

Hi @shreyashkanodia

The line temp = int(temp) is necessary because the input() function in Python always returns the user input as a string. When you ask the user to enter a temperature, the input is stored as a string. To perform numerical comparisons, such as if temp > 30:, you need to convert this string to an integer. The int() function is used to convert the string to an integer so that you can correctly compare the temperature value numerically.

Without this conversion, the comparison temp > 30 would raise an error because you cannot compare a string to an integer directly.

If you still face any issue, please feel free to get back to us.

Thanks for the clarity, my doubt is cleared.

1 Like

This comes under the topic Casting:
In python input is stored as a string, so as to do the mathematical manipulations we need to convert it into integer with the help of int().

Thank you

Thanks for the advice

Hi @shreyashkanodia
temp=int(temp)
This code statement converts data type of temp variable from string to integer as return type of input() function is string.This conversion helps to compare temp variable with 30 in next line of code
I hope it helps