0

I want all the print's in my text editor to be purple. When I'm coding it, it looks for each occurrence of print in my code, and tries to add a tag to it. Unfortunately, it's giving me an error that unicode strings don't work with it?

def convertToPyTags(self, blah=None):
    # Run every time a key is pressed, I know that works
    indexes = [] # This works
    textdata = self.texteditor.get("1.0",END) # This works
    print textdata
    print list(textdata), "L" + str(len(textdata)) # This works
    for i in len(textdata): 
        if (textdata[i:(i+7)] == " print "): # This is where it gives an error
            indexes.insert(len(indexes), i)
    print indexes

""" OUTPUT """

[u'p', u'r', u'i', u'n', u't', u' ', u'"', u'H', u'e', u'l', u'l', u'o', u' ', u'w', u'o', u'r', u'l', u'd', u'!', u'"', u'\n'] L21
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1486, in __call__
return self.func(*args)
File "script", line 145, in keyPress
self.convertToPyTags()
File "script", line 136, in convertToPyTags
for i in len(textdata):
TypeError: 'int' object is not iterable

I don't know what this means - apparently integers aren't iterable?

Again, paste bin

1 Answer 1

2

When you do this:

for i in len(textdata):

... the for loop is expecting a list of items (an "iterable"). However, you're giving it a number, which is why the error says "int is not iterable". If you want to iterate over the textdata, do for i in textdata.

Your other choice is to use range(), which will return a list of numbers in a range. So, if you want to do a loop using numbers, you can do for i in range(len(textdata)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.