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

Question

I entered my code as instructed by the exercise, but I’m seeing a TypeError in the terminal when I press Run. Why is this happening?

Answer

If you’re seeing a TypeError, take a look back over the string you built with concatenation (using + between parts to make a big string). For example, if you tried doing big_string = "The product was " + product, you’ll get this error.

To fix this, convert the variable product to a string so it can be concatenated with strings, like this: str(some_number_variable).

5 Likes

2 posts were split to a new topic: How Should I Define X?

I kept getting the TypeError code, I tried all the different layouts of the code as I could think of and still had no luck, I finally gave in and got the solution, only to find I was correct but needed to include a space at the end of the string, so rather than it being: “The product was” followed by the rest of my code, it should have been: "The product was ". Hope this helps anyone encountering this problem.

23 Likes

That would have been actually very easy to spot if you had paid attention to the right panel. Writing “The product was10.0” isn’t quite nice in general, nor pleasing to the eye.

2 Likes

I agree, I should have noticed it a lot sooner, I usually do leave a space and thought I had, but obviously I had missed it somehow. And when looking through my code, it was hard to spot the missing space (I don’t usually pay close attention to the right panel)

2 posts were split to a new topic: How to Get Rid of the Decimal Place Here?

2 posts were split to a new topic: Why is str() Necessary for Concatenation Here?

Actually, the commands
string_num = “7.5”
print int(string_num)
will not give the value of 7. Instead, it will show ValueError: invalid literal for int() with base 10: ‘7.5’.
I do not know why it is. But I figured out how to solve it. Do this instead:
string_num = “7.5”
print int(float(string_num))

4 Likes

I got the same ValueError as well.

1 Like

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”