Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Types and Branching Comparisons

Chaya Feigelstock
Chaya Feigelstock
2,429 Points

Why is the integer that is passed through "age" automatically converted into a string? What is the logic behind it?

Craig briefly mentioned when going through the code at the end that the integer passed through "age" is automatically converted into a string but didn't explain why?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Chaya Feigelstock ! This is a really common question and you're far from the first to get tripped up by this. In fact, it's so common because it applies to literally every programming language I've dealt with including Swift, C#, Python, Ruby, JavaScript and others. Whenver you send a prompt to the user, whatever they type in comes in as a string. This is true for programming languages just across the board (as far as I know and definitely those listed above). But there are numbers that are ok to have as strings, too. For example, a zip code would be a great number to have as a string. You don't need to do any math on it, or a telephone number. Same thing there.

But here, we need the age to be a number we can do math on so we have to tell it to take what the user sends in (which, again, is a string) and turn it into an integer, which is a number we can perform math on later.

And think about it for a minute. Most of the time when we're asking a user for information, we're really wanting a string (even when it's a number). We might ask for their name, address, zipcode, state, email address and more. While some of those may contain numbers, the numbers we actually need to use math on are relatively limited so it makes sense to have the information coming in from the user a string by default and then change it to a number if we need it that way :smiley:

Hope this helps! :sparkles: