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

Chris Angelico rosuav at gmail.com
Fri Apr 26 10:53:45 EDT 2013


On Sat, Apr 27, 2013 at 12:26 AM,  <tunacubes at gmail.com> wrote:
> Hey,
> 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. It worked fine when I just had it input one device into the script without the use of lists, but for whatever reason, using a list does not play nice with replacing the words "Device" with the number from the list. Here is my script. I will give you up until the part that the error occurs, as everything afterwords is pointless.
>
> I am fairly new to python, so if anything looks screwed up or like I am an idiot, it is because I am.

Welcome to Python. One thing that you'll quickly learn is that the
full traceback is REALLY helpful; in this case, there won't be much
traceback, but at least you'll get the line number. I'm looking at
this line as being the culprit:

>     line = line.replace("device", devlist[0])

All you need to do is convert that to a string:
    line = line.replace("device", str(devlist[0]))
and that should fix your problem. But while I'm here, may I offer a
few other tips?

(By the way, I'm going to assume you're using Python 3 here, because
otherwise your "confirm when ready" would be bombing. But it helps to
say what version you're using.)

> import fileinput, sys, os
> devlist = []
> maxdev = int(input("How many devices to add: "))
> curdev = int("0")

Just 'curdev = 0' would work here. No need to convert a string to an
integer - just use a literal integer.

> while curdev < maxdev:

UI suggestion: Let the user keep on inputting device numbers until
some kind of sentinel (eg a blank line), unless it makes really good
sense to ask for the number up-front.

>     try:
>         Number = int(input("Enter Device number: "))
>         devlist.append(Number)
>         curdev = curdev + 1
>     except ValueError:
>         print("Please enter a valid number")

You can save the hassle of maintaining curdev by simply looking at
len(devlist) - that is, change your condition to "while len(devlist) <
maxdev".

> ready = 0
> while ready != "Y" and ready != "y" and ready != "yes" and ready != "YES" and ready != "ready" and ready != "Ready":

Here's a really cool tip. You can simply check set membership:

while ready not in {"Y","y","yes","YES","ready","Ready"}:

But I'd be inclined to make the UI a bit tighter here and make it
something like:

ready = input("Confirm when you are ready <y/N>: ")

and then just look for either "Y" or "y", nothing more. But that's up
to you. You know who's going to use this program, I don't. (The
capitalized N representing that the default is No is a convention you
may or may not want to follow.)

>     try:
>         ready = input("Confirm when you are ready ")
>     except ValueError:
>         print("shit broke. ")

Drop the try/except; if there's something wrong (and, btw, I'm not
sure what circumstances would trigger a ValueError here - maybe
someone else knows of something?), just let the exception terminate
the program. There's nothing useful to do here anyway :)

> ##This next step will seek out the word Device within firstdev.ahk, and replace with devlist[0]
> for line in fileinput.input(["firstdev.ahk"], inplace=True):
>     line = line.replace("device", devlist[0])
>     sys.stdout.write(line)

You're reading a single file with constant name here. Save yourself
some trouble: just open the file directly.

for line in open("firstdev.ahk"):
    print(line.replace("device", str(devlist[0])))

Though I'm not sure why you want to write to stdout here. Were you
intending to write back to another file? I'm getting a bit lost in
your code here. My understanding of your intentions, from your
comments, is that you actually want to repeat most of the code from
here on for each device number entered - that strongly suggests a
'for' loop bracketing the whole thing. Is that what you meant this to
do? Currently, it'll get to the 'del' at the bottom, and then just
terminate.

> ##next step runs firstdev.ahk
> os.system('firstdev.ahk')
> ##next step is replacing devlist[0] with "device"
> for line in fileinput.input(["firstdev.ahk"], inplace=True):
>     line = line.replace(devlist[0], "device")
>     sys.stdout.write(line)

And here you repeat everything from above. Again, not sure what you're
intending to do here.

> del devlist[0] #deleting the first item from the list. next steps will repeat.

The steps won't repeat by themselves, so this is where I'm thinking
you actually want a for loop.

I hope you don't take this the wrong way, as I feel now (on skimming
over this email prior to sending) that I've kinda pulled your code to
pieces a bit! It's just advice, just suggestions; this is your code
and nobody else's. You may very well disagree with any or all of what
I've said, and that's to be expected. It's also a great way to get
discussion going, and discussion is one of the best ways for everyone
to learn :)

Chris Angelico



More information about the Python-list mailing list