How can I print a sentence with the calculation?

I tried to type this:

quilt_width = 8
quilt_length = 12
print("total squares needed:" + quilt_width * quilt_length )

To show the value in the same line as the text without using 2 prints.

And I got the following error:

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    print("total squares needed:" + quilt_width * quilt_length )
TypeError: Can't convert 'int' object to str implicitly

I’m not sure what I did wrong. What is the correct syntax to achieve what I wanted?

Am I getting ahead of myself and this is something I will learn down the line?

quilt_width * quilt_length gives an integer (96). Trying to concatenate str + integer gives errors, as you can see in the error:

TypeError: Can't convert 'int' object to str implicitly
2 Likes

Can you explain more please?

What’s the correct way to write it then?

Is using two prints the only way?

You use + to attempt string concatenation, but you don’t have two strings. Thus you get an error.

So either you will need to convert the integer to a string, so you can stick with your current approach (using +).

Or the must be another way to solve this, but i don’t know what you have learned. Have you learned string Formatting Operator? Or the string .format() method? Or the use of a comma?

1 Like

No, not yet.

I’m still at the very beginning of Python.

So far, I learner these things:

  • Comments
  • Print
  • Strings
  • Variables
  • Errors
  • Numbers
  • Calculations
  • Changing Numbers

I will google the error I got and try to understand what happened, you already gave me some hints.

I guess I still lack the basics, I will understand what I did wrong after few more lessons.

Thank you for your time.

so you had string and numbers, that is good.

The problem is that using + to add number and string together, will result in a TypeError, you can add these two types together. these types don’t go together, just like certain things don’t go together.

you can use a comma (,) instead of a + to overcome your problem. I had no idea this exercises was so early in a course. Had i known, i would have simplified my answer. Sorry about that

3 Likes

This makes more sense to me now as a newbie hahaha

Thanks, I will try your solution and Google to learn more about it.

Thanks again.

6 days later, I understand the error I made.

I was trying to add a number to a string, like saying 8 + trees equals what? Which doesn’t make any sense logically.

I changed my syntax to since you mentioned a comma:

quilt_width = 8
quilt_length = 12
print("total squares needed:" , quilt_width * quilt_length )

And it worked just fine.

6 Likes

Found comma(,) function was useful. Thanks @stetim94

If i want to print a str along with a calculation, then how do i do it?
i.e. like
quilt_length = 12
quilt_length = 8
squares = “no. of squares needed now”
print(squares + (quilt_length*quilt_width))

This shows a TypeError, how to rectify it?

you could use format:

https://pyformat.info/

or f-string would be even better, but that requires python 3.6+