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
Roland Brenevil
189 PointsI entered the following code in the console (exact code being thought) but I am getting a TypeError
praise = "You are doing fine" praise = praise.upper() number_of_characters = len(praise) result = praise + ":" + number_of_characters print(result)
4 Answers
Eleeza Amin
34,496 PointsHello Roland,
The TypeError is from attempting to concatenate a string and an integer.
praise = "You are doing fine" #This here is a string.
praise = praise.upper()
number_of_characters = len(praise) #This here is an integer.
result = praise + ":" + number_of_characters #So this can't happen.
print(result)
All you need to do is use the str() method to turn the integer into a string. To do this, simply go to the line:
number_of_characters = len(praise)
and put the len(praise) inside the str() method like so:
number_of_characters = str(len(praise))
This will turn 18 into "18"
Hope this helps! :)
Roland Brenevil
189 Pointsthank you
Eleeza Amin
34,496 PointsAny time :) Happy to help.
KRIS NIKOLAISEN
54,974 PointsYou have a type error because you are attempting to add an integer to a string. To concatenate convert the integer to string first using str() like this:
result = praise + ":" + str(number_of_characters)
Eric M
11,547 PointsHi Roland,
It might help if you link the video that you're having trouble with.
The issue with the code that you've posted is that you can't perform concatenation operations (joining strings together with a plus sign) with integers and you can't perform addition operations (adding numbers together with a plus sign) with strings - you have a string praise, a string literal ":" and an integer number_of_characters with plus signs between them.
If you cast your integer to a string you can then concatenate the textual representation of its value to the end of the string. You can cast simple values in python by wrapping them in the appropriate type. So to turn an integer called temp_value into a string you would use str(temp_value) and to turn a string of number_guessed_by_user into an integer you would use int(number_guessed_by_user).
e.g.
result = praise + ":" + str(number_of_characters)
Best of luck and happy coding,
Eric
Robin Goyal
4,582 PointsIt seems like you're trying to run the script while being in the Python shell. Exit the shell by typing quit() or pressing ctrl+D and then running the script while being in the console (when you see the $ instead of >>>). I believe that might be the issue based on your error information that you posted.
Robin Goyal
4,582 PointsYou also have the issue of adding a string and an integer together while will give you the TypeError.
Eric M
11,547 PointsThis is a good catch from Roland's comment to his question, Robin, thanks!
Roland Brenevil
189 PointsRoland Brenevil
189 Pointspython functions.py
File "<stdin>", line 1
python functions.py
^
SyntaxError: invalid syntax