Beginner question: why is something expressed as integer or string in particular circumstances

skill_completed = "Python Syntax"
exercises_completed = 13
points_per_exercise = 5
#The amount of points for each exercise may change, because points don't exist yet
point_total = 100 
point_total = 100 + (exercises_completed) *(points_per_exercise)
print("I got "+str(point_total)+" points!")

Hello Code Academy. I am an absolute beginner to Python and coding in general, as well as a beginner to these forums. So, please let me know if I breached any forum rules or anything like that.

My question is about stage 14/14 “Review” of “Python Syntax” in “Learn Python 2”. Link here and code written above.

What I have learnt so far is that Python can translate different categories of code, an integer into float, for example, or integer into string (or vice versa), in this specific case.

That’s why I am confused about the last line of code above. As I understand, quotation marks make “Python Syntax”, for example, a string (in the first line). Whereas 13 or 5 in the second and third line are integers because they do not have quotation marks and no decimal points so are neither strings nor floats.

Given this information, I am confused as to why the last line of code expresses (point_total) as a string (str is short for string?). Shouldn’t this be expressed as an integer, because we are working with numbers, without quotation marks (point_total equals or corresponds to the integers 13, 5 and 100 in this code).

Therefore, the last line of code seems like it should be int(point_total) not str. However, this did not work: the error message produced was TypeError: cannot concatenate ‘str’ and ‘int’ objects

I hope this question makes sense. Thanks very much in advance for any attention towards this post!

Hi,
You’re right, point_total is an int. What the str( ) is doing is temporarily changing its value to a string. This is because concatenation (chaining together with + ) only works with strings.

So, if point_total equals 100, the line effectively becomes;
print (“I got " + “100” + " points!”)
and it’s now joining three strings together, rather than trying to join 2 strings and an int - which it doesn’t like, hence the error message.

As I say, in this situation it’s just a temporary change to make the print statement work. point_total will still be an int afterwards.

Hope this helps

2 Likes