# Why can I write age = int(value) but not print (int(variable))?

Hello,

Today my question is about data types (which for me is a very confusing topic).

Context: I was writing a program that takes a dog’s age in human years and converts it into dog years. Because for the first 2 years a dog’s age is equal to 10.5 years in human years. and for the rest of it’s life it is equivalent of 4 human years. So I wanted to write a program that takes your dogs age as input and tells you how old your dog really is.

Sample input and output:
Input: 15 (Dog’s age in human years)
Output: 73 (Dog’s age in dog years)

Code:

``````# if the age is less than 2 then age*10.5 and print. If the age is more than 2 then, (2*10.5) + (age-2)*4 then print

age = int(input("Enter your dog's age: "))

if (age<3):
age = int(age*10.5)
print ("Your dog's real age is: ", age)
elif (age>2):
age = int((2*10.5) + ((age-2)*4))
print ("Your dog's real age is: ", age)
``````

Output:

``````Enter your dog's age: 15
Your dog's real age is:  73
``````

Question: My question does not lie in the code but in an error of the code. If I wrote the code like this below:

``````# if the age is less than 2 then age*10.5 and print. If the age is more than 2 then, (2*10.5) + (age-2)*4 then print

age = int(input("Enter your dog's age: "))

if (age<3):
age = age*10.5 #Here is the difference in this code
print (int("Your dog's real age is: ", age)) #Here is the difference in this code
elif (age>2):
age = (2*10.5) + ((age-2)*4)
print (int("Your dog's real age is: ", age))
``````

Then I get this:

``````TypeError: 'float' object cannot be interpreted as an integer
``````

Now my questions are:

1. If a float object can not be interpreted as an integer, how does it work in the first code? and Why?
2. Also, The Python TypeError is an exception that occurs when the data type of an object in an operation is inappropriate. Then why does it occur here? Like all the functions that can be performed in an integer can be performed in a float right? So even if there was a mistake, shouldn’t it be some different type of error and not type error?
3. Are the same principles applied for all the programming languages, or like all oop languages or just python specifically?

Sorry I write multiple questions in a single post, I don’t know if it is appropriate or not. I start out with one question then a few others related to that one comes to my mind. Thank you to all of you for being patient and reading my posts and answering my questions.

Thank you
Joyeta

When you put your sum in brackets of the function int, python converts it to an integer. In the second line you printed ‘age’ as an int when in fact in the second code age is a float because it hadn’t been converted to an int. Sorry I can’t help you farther, Its seems to me that its alright to post multiple questions on one topic as maybe someone a little more knowledgeable in python then me can answer them for you.
This might be helpful for solving second question:

As for your third question, java script treats all numbers as floats and some by languages you need to define your number by its type. Each language is different and while they there similarities they don’t all treat data types in the same way.

This is quite interesting issue.

You are right that it’s fine to convert float to integer with ie. `int(4.2) # 4`. No problems with that. You are also right that error is rather strange.

The call to the `int` in the second example looks like:

``````int("Your dog's real age is: ", age)
``````

Here are two arguments, `str` and `float`. If it would be just first argument - `int("Your dog's real age is: ")` - the error would be:

``````>>> int("Your dog's real age is: ")
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
int("Your dog's real age is: ")
ValueError: invalid literal for int() with base 10: "Your dog's real age is: "
``````

Why then adding a `float` argument makes it `TypeError`? Very small hint is the above error. `int` can convert to a numbers having different `base` than `10`. In such case the base is passed as a second argument. Because of that second argument is expected to be `int`. As in the call second argument is `float` instead, then `TypeError` is raised.

There’s at least couple situations when using `float` in place of `int` is not okay. Another case that come to mind is array indexing. Although a more helpful error is raised. ie.:

``````>>> [1, 2, 3][1.0]
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
[1, 2, 3][1.0]
TypeError: list indices must be integers or slices, not float
``````