Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Comparison Solution

Damien Ellis
Damien Ellis
199 Points

Comparing two similar Codes! One works while the other does not. Why?

This Code Works:

name = input("Please enter your name: ")

number = int(input("Please enter a number: "))

print("Hey", name + "!")

print("The number is", number, "!")

But

This Code Does Not:

name = input("Please enter your name: ")

number = int(input("Please enter a number: "))

print("Hey", name + "!")

print("The number is", number + "!")

I mainly changed the "," and the "+" and it stopped working properly.

Why????

1 Answer

Josh Keenan
Josh Keenan
20,315 Points

What you are adding are different types, but before concatenation. In the first instance you are essentially formatting the integer into the string. In the second case you are trying to add (the mathematical operation) a string and an integer together, and Python doesn't know what to do in that case, so throws an error instead. Hope this helps and feel free to ask any questions.

Damien Ellis
Damien Ellis
199 Points

Thank-you! I understand it now. I appreciate the quick reply!