How can I print a sentence with the calculation?

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.

1 Like

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

19 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.

22 Likes

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

1 Like

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+

1 Like

Use a comma(,) instead of a +
That works.

Thank you, bro

1 Like

I later also replied this:

which i can highly recommend

oh i dont no im sorey

What if you convert the evaluated integer to a string by wrapping the result in the str() function, and concatenated that? This would explicitly say that you want to convert the integer to a string so you can concatenate it with the sentence.

print("Total squares needed: " + str(quilt_width * quilt_length))
6 Likes

si quieres concatenar texto con numero,necesitas convertir el numero a texto, con la funcion str() ejemplo :
print("total de cuadrados necesarios: " + str(quilt_width * quilt_length))

1 Like

@saidbout Thank you for asking this question, was real cool reading all the responses printing strings and integers together ! Its awesome you were thinking of combining the two at the time :+1:
@stetim94 @jimzoo9423838668 @ethanrogers484583614 @object1407568875
A year later, I appreciate all your help :pray:

1 Like

The TypeError: can’t convert ‘int’ object to str implicitly is generated because ‘+’ (addition) does not work between an integer and a string. When concatenating strings with integers - you can’t directly stick together a string and an integer. So, in order to resolve this problem, you have to explicitly parse the integer to a string by the str() built-in function string + str(number) or use formatting to format your output.

There are 3 ways to resolve this problem.

print (‘your string:’ + str(value))
print (‘your string:’, value)
print (‘your string: {}’.format(value))

1 Like

Here is the simplest solution to the your problem :
quilt_width = 8

quilt_length = 12

print(“total squares needed:” +

str(quilt_width * quilt_length) )
i order to combine (concatenate) strings using “+” operator the operands must be of string type . in the code above you can see that i print statement one operand is string type and other is int type so i have converted the int to sting using “str” function and hence you would be able to concatenate two strings (As for strings to be concatenate both the strings has to be strings)

you can do a concatenation of by adding the str(int) when you do the calculation. see below what i did to get the it to the print “You will need 64 total squares”.

if you do run the code below you should get both print and calculation together. though i am trying to figure out how to add a space in between.

quilt_width = 8

quilt_length= 8

#print “64”

print (‘You will need’ + str(int(quilt_length * quilt_width)) + ‘total squares.’)

I think you can insert a space inside quotes after the word “need” and colon, and also inside quotes before the word “total”. :slightly_smiling_face:

as @system2120220371 mentioned u can do this
print (“You will need " + str(int(quilt_length * quilt_width)) + " total squares.”)

When I try to print some text in the “Codecademy compiler” it gives an error like this:


Is this the compiler problem or my code is wrong?

Thanks for the suggestion