TypeError: Can't convert 'int' object to str implicitly

Peter Otten __peter__ at web.de
Fri Apr 26 10:53:44 EDT 2013


tunacubes at gmail.com wrote:

> Hey,
> 
> Let me explain what my program is supposed to do...
> 
> I am using a macro program in conjunction with a python script I am
> writing. The macro inputs data into a database we use at my job, blah blah
> blah.
> 
> The script asks how many numbers (devices) you need to enter. Next, it
> asks you to input the device numbers. When you input them, it creates a
> list with all of the devices. I then tell it to go into the script of the
> Macro (firstdev.ahk) that will run on the back-end, and replace the word
> "device" with the first device in the list. It then should execute the
> Macro, change the device number back to the word "Device" for future use,
> and then delete the first number from the list. It will repeat as long as
> there are numbers in the list.
> 
> The error I receive is "TypeError: Can't convert 'int' object to str
> implicitly" when it tries to put the device into the macro script. 

Python is trying hard to give you a meaningful error message and shows the 
line causing the error in the traceback. It pays to read carefully -- or to 
post it here if it doesn't make sense to you.

> devlist = []
...
>         Number = int(input("Enter Device number: "))
>         devlist.append(Number)
...
>     line = line.replace(devlist[0], "device")

devList is a list of integers, and devlist[0] is thus an int.

>>> "foo device bar\n".replace(42, "device")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

Implicitly? So let's try and convert the int /explicitly/ .

>>> "foo device bar\n".replace(str(42), "device")
'foo device bar\n'

No error, but probably still not what you expected. Can you sort it out 
yourself?

> I am fairly new to python, so if anything looks screwed up or like I am an
> idiot, it is because I am.

I like to see a bit of self-deprecation now and then, but on this list 
complete tracebacks find even more love ;)





More information about the Python-list mailing list