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

I continued to receive error, which stated the my statement read : The product was 10.0 we wanted it to say “The product was 10”- I had the proper spacing in my typing and all that, yet when I clicked the solution the only thing that was changed was i needed line 3 to be empty…my question is, did they want me to get rid of the “.” in the float 10.0 and make it an integer? If so, what does making line 3 blank have to do with changing it 10.0 to 10? Am I missing something?

1 Like

float_1 = 0.25
float_2 = 40.0

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

So here was the correct answer for this problem. I had the same exact thing the only difference was that I had not skipped a line in the code. Can someone explain why I kept getting error message?

1 Like

Does programming always make a person feel so stupid? Haha thanks for the solution. It was so easy, and I knew it, my mind was just stuck on X and it obviously should have been focused on Y.

2 Likes

as beginner programmer, its so easy to go down the rabbit hole (and then complete in the wrong direction)

i think its quite common for people who start with programming, its not just about code, its also about a mindset, understanding the computer and so forth.

2 Likes

im so lost. there is all the proper spacing in my code, yet its STILL telling me my answer is " The product is10.0 " and ive rewritten my line of code so many times only for it to give the same error!! whats going on here?

please see this topic:

How to ask good questions (and get good answers)

Without the code, its really difficult (nearly impossible) to tell what is wrong

ive seen many others not share the full code yet received replies like it was nothing. im stuck at the exact same place everyone else is. so y is that not good enough?

they also dont get mods telling them how to ask for help.

the full code makes helping even easier, but only the relevant part is also fine.

the topic i shared is in at least 120 post, so its quite commonly used, and there is a reason we use it. Really allows us to help learners better, that topic (how to ask good questions) was made based on years of experience

i was in other threads and never saw that one being posted in a direct reply to another. here i am thinking someone is gonna actually help, yet im being directed on how to ask for it. :confused: :disappointed:

3 months later and i too have the same issue with the same code. i look at the solution and everything is identical. so how come my print is showing as “The product was10.0” and not “The product was 10”

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.
else:
    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!

7 Likes

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

1 Like

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