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:
- If a float object can not be interpreted as an integer, how does it work in the first code? and Why?
- 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?
- 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