Python Forum
Why is this an error? (string and integer)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this an error? (string and integer)
Why is this an error?

"The correct answer to this multiple choice exercise is answer number " + 2
TypeError: Can't convert 'int' object to str implicitly
Why doesn't Python simply print the string and the integer together as the code instructed?

Python is not like some other languages like javascript, you need to cast the int to str in order to perform concatenation. Just accept it. This yields to ugly code. That is why more advanced str.format() or f-string are preferred and they offer more advantages. check and string formatting mini-language.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Quote:Just accept it

Function of adding is example of polymorphism (the ability of different objects to respond to the same message differently).

In Python context this means that function of adding is defined in different datatypes. Adding can be used on different datatypes with different results.

Adding a + b is 'syntactic sugar' on a.__add__(b). This means that any class which has implemented __add__ method supports adding (between instances of the class). For integers it's 'classic' adding: 1 + 1 -> 2, but in case of lists it's actually concatenation [1, 2] + [3, 4] -> [1, 2, 3, 4]

Due to polymorphism adding is not supported between all datatypes i.e instances of all datatypes (obviously it supported between numerics) because it is defined to deliver different results. If you have adding and concatenation it's hard to find common ground and "In the face of ambiguity, refuse the temptation to guess."
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
I like TypeErrors.

With JavaScript you can do this:
Array(16).join("wat" - 1) + " Batman!";
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 536 May-24-2021, 08:48 AM
Last Post: bowlofred
  How to check if user entered string or integer or float?? prateek3 5 7,755 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 1,094 Oct-14-2019, 10:40 AM
Last Post: perfringo
  Error when entering letter/character instead of number/integer helplessnoobb 2 1,819 Jun-22-2019, 07:15 AM
Last Post: ThomasL
  Mixed string,Integer input variable issue maderdash 2 1,394 Nov-06-2018, 09:46 AM
Last Post: snippsat
  string with integer variable within Andy1845c 9 2,521 Mar-12-2018, 10:28 PM
Last Post: Larz60+
  Convert integer to string Maigol 5 3,610 Mar-05-2018, 10:25 PM
Last Post: python_master89
  cannot convert float infinity to integer error despite rounding off floats Afterdarkreader 3 10,861 Dec-15-2017, 02:01 AM
Last Post: micseydel
  why does integer input give error invalid literal for int() PyPhanman 2 4,624 Apr-27-2017, 05:32 AM
Last Post: ndc85430
  Error "an integer is required" in getpass.getuser(). Nirelg 5 3,422 Mar-20-2017, 04:19 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020