Syntax - ValueError


#1

I don’t understand my error

product = float_1 * float_2

print "The product was" + int(product)+"!"

Here is the error message

Traceback (most recent call last):
  File "script.py", line 6, in <module>
    print "The product was" + int(product)+"!"
TypeError: cannot concatenate 'str' and 'int' objects

Could you help me understand what I am doing wrong?


#2

The error thrown tells you that you cannot concatenate (this + that) a string with an integer object.

Your string is “The product was X”

And your int is product
# int(product)

But you’re expected to concatenate a string with another string.
So, instead of an integer, what could you change in order to concatenate your string with a string object?


#3

Thanks a lot it makes sense. Correct answer is

product = float_1 * float_2

print "The product was" + str(product)+"!

I am trying too hard, overcomplicating things when the answer is simple.
Thanks!


#4

I don’t understand my Error either and I tried different combination… the exercise is:

Create a string called product that contains the result of multiplying the float value of float_1 and float_2.

And this is what I did

float_1 = "2."
float_2 = "5."
product = float(float_1) * float(float_2)
product = str(int(product))

The error says: “You set product to: 10 when it should be 10.”

…errr :sweat_smile::sweat_smile:

product = float_1 * float_2

print "The product was" + int(product)+"!"

Here is the error message

Traceback (most recent call last):
  File "script.py", line 6, in <module>
    print "The product was" + int(product)+"!"
TypeError: cannot concatenate 'str' and 'int' objects

Could you help me understand what I am doing wrong?
[/quote]


#5

I submitted a bug report for this problem due to the confusing language in the instructions. In your case; however, you have a valid error that the bug report won’t fix, as well as unnecessary lines of code.
Remember that Python will automatically determine the datatype based on how you format what you are setting your variables to. This is usually the preferred method of assigning datatypes. Thus, if you want an integer, you would type x = 5, if you want a float: x = 5.0, and finally a string: x = “5” or x = “5.0”. For me, the float values were already assigned as floats to float_1 and float_2, so I am not sure why you redefined them in your code. Try changing how you are assigning your datatypes and then do your product and see if that gets you closer to the right answer.
As for the error saying product should be 10, that is a large part of why I submitted the bug report, so don’t worry about that too much.
And lastly, as far as the actual traceback error you are receiving, that is because of the same reason that has already been answered here. The + operator expects expressions of the same datatype on both sides of the operator. If both sides are numbers, it performs an addition, if both are strings, it concatenates them. If you have one of each, it can’t tell if you really meant to add two things together or if you wanted to make a string from two other strings, so it throws an error to tell you to make them both the same before you can move on.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.