Why does data type conversion in python not work without creating a new variable?

Viewed 47
age = input("how old are you? \n")
int(age)
print(type(age))
# <class 'str'>

age = input("how old are you? \n")
age_as_int = int(age)
print(type(age_as_int))
# <class 'int'>
1 Answers

When you convert a data type to another using str(), int(), float() you need to assign the value to a variable, as you can see they are functions, they return something and if you don't put that something into a variable it gets lost.

for your first code to work it should be:

age = input("how old are you? \n")
age = int(age)
print(type(age))

or maybe

age = int(input("how old are you? \n"))
print(type(age))
Related