Why does my code give a TypeError when I press Run?

I have been on this forum for years, and seen many super users (TL3), moderators and others ask clarification questions so we can help you better.

what is the problem to share the exercise url and your code?

1 Like

@harleyquinn420, if I come on to a forum and say something like,

“I typed everything right, but it is telling me my answer is ‘xyz’”

How can you know what I typed? How can you know what the exercise was?

There is no way to answer a question like that.

If you help us, we will try to help you.

1 Like

Regardng the ‘Value Error’ exercise, I found it to be confusing and the so-called proper response to be incorrect. I, too, typed everything just as it is displayed and kept receiving a syntax error message. I finally just typed in the following and the error message went away.: big_string = “The product was 10”.
This is not the way the lesson detailed setting the variable values (i.e. quotation marks are not used). I also tried to convert product to a string as in the example to no avail.
I also found no responses addressing this specific issue. Furthermore, most of the responses to questions asked are unclear and only lead to more questions – specifically, what the heck are you saying that actually answers my question in a clear, concise manner. If you do not know the answer, say as much. The online help is sorely lacking.
I would like someone to explain to me in clear, concise, coherent and complete sentences why my ultimate response to the exercise is correct and cause the error message to go away. I would also like to know an alternative response that would have worked. Once again, my response to the exercise is big_string = “The product was 10”. I should have been able to enter, big_string = The str(product) was product. What this should do is change “product” to a string and placed the value of ‘product’ in the place of X.

I’ve experimented with it a bit, and it appears that algorithm that tells the grader to accept or reject an answer has been programmed to accept either big_string = “The product was 10” or big_string = “The product was 10.0”. It will not accept big_string = “The product was 9” or big_string = “The product was product”.

It’s as simple as that: The grader program is something like:

if big_string == "The product was 10" or big_string == "The product was 10.0":
    show green check on check-box number 2 and light up the yellow "Next" button.
    show red error message

So… The problem seems to be, how to compose a passing string to assign to the variable big_string.

Well, you have clearly already found one way, but, as you correctly surmise, that probably isn’t what the authors of the exercise had in mind: They want you to build up the string; there are many ways to do that, but they want you to do so using concatenation (i.e., using the + operator with strings.)

One difficulty seems to be that the problem uses the word product in two ways: as a string, "The product was ", and as a variable, product = float_1 * float_2.

Those two are separate, and unrelated. The variable name could have been bumblebee or olive, and the concepts would be the same:

olive = float_1 * float_2
big_string = "The product was " + str(olive)

… will pass the second part and get a “Next” just fine! So the “product” in the string and the variable named product have nothing to do with each other. It is a coincidence that they both consist of the same sequence of symbols.

When used with strings, + must have a string on both sides. That is the take-home lesson.

Hey everyone! I’ll break this down since the lesson didn’t outline this clearly:

Our variable called “product” is producing a float value because both variables stored inside of “product” have decimal values and are not integers. So when you write the following code, it prints the float value:

big_string = "The product was " + str(product)

The product was 10.0

If we want big_string to print our product variable as an integer, you have to use int() instead of str().
But this produces an error stating that you can’t concatenate (combine) a string with anything other than a string. The following variations of the code produce this issue:

big_string = "The product was " + int(product)

TypeError: cannot concatenate ‘str’ and ‘int’ objects

big_string = "The product was " + float(product)

TypeError: cannot concatenate ‘str’ and ‘float’ objects

The answer is that you have to nest your string modifiers and also make sure you close the new string properly with a double end parenthesis:

big_string = "The product was " + str(int(product))

The product was 10

This converts our variable into a string but then also converts the variable into an integer, which is what the test wants us to reproduce.

It was not clearly outlined in the lesson that we can nest our modifiers but this is also a good way to stretch our limits and teach us things!


Plain and simple, the issue that I and everyone else was having is due to there not being a space before the last quote. “The product was(spacebar)”. A space between the word was and the ".


Yes, I also received the same error! Can anyone please clarify this?

I know this question might not be the best to ask here…
The thing is that back in the day when I started learning Python 3 I remember that for print() elements I had to use the , for concatenating strings with variables or numerical values. The thing is that in the variable I tried to do the same and appears to be wrong.

My question is, is this because it’s Python 2? Or because inside a variable you can’t concatenate elements with a coma? I’m in the lesson 13. Value Error of Python 2 First Module.

The solution is big_string = "The product was " + str(product) but I was wondering why could it be wrong to be big_string = "The product was ", product

I hope I’ve explained correctly. I’m just doing this course for the love to programming and because I’m a nerd that finishes all the things I start. But if it’s not your case drop out this course. Python 2 doesn’t exist in modern programming and will only give you headaches when going to Python 3 (in few aspects). I only know something about Python 3 though :stuck_out_tongue:

Have a nice day!

using , in print doesn’t concatenate the string. Function parameters/arguments are separated by comma’s. So using , with print will simple print multiple arguments pass to the function.

to concatenate a string, you have to use +.

1 Like

Here is my question:

In 13.2, it is written as, “create a string called big_string”. This has made me so confused. I mean big_string is a variable not string, it contains the answer that IS a string.

Please rectify if I am incorrect.

I’d take the phrase “called x” to be similar to “named x”. So you want to create a new string with the name/identifier of big_string.

If it was phrased create a string containting big_string or something along those lines then it’d be describing the contents of the string itself.

Thanks man, it’s so funny now when I see it, but I was stuck for 10 min.

"The product was "
make sure to made space and add+ and str()

  1. The teaching example in this ValueError lesson page gives incorrect information.
    It states explicitly that the following two lines of code:

string_num = “7.5”
print int(string_num)

will produce the output of 7.
This is clearly incorrect, the output will be an error:

Traceback (most recent call last):
File “script.py”, line 2, in
print int(string_num)
ValueError: invalid literal for int() with base 10: ‘7.5’

I have reported this in the bugs & mistakes area of the lessons.

  1. In my testing, you can convert this string to a float, AND you can convert that float to an integer:

string_num = “7.8”
print float(string_num)

The output is: 7.8

string_num = “7.8”
print int(float(string_num))

The output is: 7

This seems to imply that you cannot directly convert a string to an integer with the int() function (although you CAN convert a string to a float and then to an integer).
Is this correct?

(EDIT: If the string variable is “7” (i.e. a string that imitates an integer type), then int(string_num) will work.
If the string variable is a string that would resemble a float, for instance “7.5” or even “7.”, then int(string_num) will throw an error.)

ditto, u need a space after was before the quotation marks

~To get 10 instead of 10.0 you need to type,

       product = float_1*float_2

       product = int(product)

~ To get a space between “was” and “10” type,

       x = " "

       big_string = "The product was" + str(x) + str(product)

       print big_string

Now you can do it
Good Luck!

float_1 = 0.25
float_2 = 40.0

product = float_1 * float_2

big_string = "The product was " + str(product)

print(big_string) # this is for printing the results. If you call the up function above it won’t print no results.