Reputation: 3
I'm new to python and I'm trying to do a simple application (age calculator); I'm using Python 3.
This is my code:
date=2012
age=input("type in the date you born to calculate your age")
print ("your age is ") + (date-age)
It seems fine to me, but it gives me a TypeError: cannot concatente 'str' and 'int' objects
.
Upvotes: 0
Views: 371
Reputation: 1
First thing you want to do is keep everything you want to print within the print() function brackets.
print ("your age is ") + (date-age)
Will not work.
This may work better
print("your age is" + str(date-int(age)))
As you move on with python you will realize why you can not do what you did with the print function. Anyway I hope this was helpful for you. Also you may notice in the code I used some functions; str() and int(). These are type conversion function. You may have came across these before or you will do very soon.
Upvotes: 0
Reputation: 8818
Try:
print("Your age is ",date-int(age))
str
will cast your integer result to a string so that it can properly be "added" to the rest of the string.
Upvotes: -7
Reputation: 1121196
Pass everything as a series of arguments to the print
function:
print("your age is", date - int(age))
The print()
function will convert the result of date - int(age)
to a string for you. Note that you need to turn age
(a string) into an integer first before you can subtract it from date
though.
Upvotes: 12
Reputation: 1359
The issue here is that you're trying to concatenate a string and an int. That is not supported by Python (or most other languages), and that is what the error message is telling you. What you've done is wrong because you are mixing up two different concepts
Incorrectly called the function - you should call it this way.
print('Your age is: ', date-age)
You used the + operator. This is an alternate method for concatenating a string and number, however to do that you have to first make sure they're both of the same type. In this case - String. You can do this by using the string function.
print('Your age is: ' + str(date-age))
A better way to have done this would be by using string formatting, mainly because it supports various formats without the need to convert them into strings as well as making a long string of text with multiple values easier.
print('Your age is: %d' % date-age)
You can read more about string formatting here.
:)
Upvotes: 1
Reputation: 544
As you learn python, it's a good idea to take the error as it appears on the last line and feed that to a search engine.
TypeError: cannot concatenate 'str' and 'int' objects
is by no means unique.
TypeError: cannot concatenate 'str' and 'int' objects
Upvotes: 1
Reputation: 6132
input
is going to give you a string. 2012, however is an int. They need to both be of arithmetic types to do mathematical operations on them. You want input to be a number, probably an int. Cast it as such with int(age)
.
So you would do print("Your age is ", date - int(age))
To nitpick your code, what if I was born in December 1992? Then your code would say I'm 20 even though I'd actually be 19. Also, what if I type in the actual date I was born, June 6, 1992
?
These aren't relevant if you're just getting started and learning the syntax, but it's good to think about because you'll quickly find that those kinds of things are what will actually give you problems in programming, while the basic syntax and little technicalities tend to be things that you can look up on Google or use a cheat-sheet for (my preferred approach since I work with so many different languages with C-style syntax) after you gain familiarity with the language.
Upvotes: 1
Reputation: 5555
Python is strongly typed so you need to convert your data to the appropriate type.
age
is a str
(string), because it comes from an input. You should write:
date - int(age)
print ("your age is ") + (date-age)
is not going to work for two reasons:
print
in python 3 is a function so it only consider print ("your age is ") + (date-age)
as its argument list;
Again, you're concatanating a str
and an int
, which is illegal in a strongly typed language.
The last conversion can be overridden since print
does all the job for you:
print("your age is ", date - int(age))
Upvotes: 1