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


#1

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


FAQ: Learn Python - Python Syntax - ValueError
#2

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


#4

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.


#6

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.


#7

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)


#8

You set big_string to: the product was 10.0 when it should be “The product was 10”.
yet I tried str and that is the only conversion it will allow. and i can not get rid of the .0