0

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.

3
  • Do you know about types of variables such as strings and integers? Commented Sep 25, 2012 at 14:23
  • possible duplicate of TypeError: cannot concatenate 'str' and 'int' objects Commented Sep 27, 2012 at 20:41
  • I don't think it's a dup; that poster apparently knew that you can't add a string and an integer, but didn't know enough about operator precedence to realize that he was adding a string and an integer. Commented Mar 19, 2013 at 19:16

7 Answers 7

12

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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:

    1. print in python 3 is a function so it only consider print ("your age is ") + (date-age) as its argument list;

    2. 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))

Comments

1

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.

Comments

1

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

Comments

1

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

  1. Incorrectly called the function - you should call it this way.

    print('Your age is: ', date-age)
    
  2. 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.

:)

3 Comments

+1 but perhaps you could say why string formatting is better? (and link to the relevant part of the doc?) :)
If you're going to recommend string formatting, recommend new-style {} formatting, not old-style '%s' formatting. If you look at the docs on old-style formatting, the very first thing you see is a big *Note saying "The formatting operations described here exhibit a variety of quirks that lead to a number of common errors… the newer str.format() interface helps avoid these errors, and also provides a generally more powerful, flexible and extensible approach…"
I wasn't aware of it. But I'll certainly look into it and improve my answer accordingly. Thanks :)
0

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.

Comments

-7

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.

6 Comments

This is not going to fly in Python 3; print() is a function, you are now adding None to a string as print() returns nothing.
@MartijnPieters whoops. Editing.
You cannot do date - age, they're different types (date is str, while age is int). As I wrote in my answer it should be date - int(age). Also the str casting isn't needed because print converts its arguments to str by itself.
@NadirSampaoli Whoops - didn't look at how date and age are set up. Fixed
As @NadirSampaoli says, doing a str around an argument to print is at best unnecessary, and can be misleading.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.