سؤال

When I try to run the following bit of code, I get the error TypeError: object of type 'NoneType' has no len() (referring to the variable stringConverted). I've tested different values and it works for the ones I've tested. I was thinking that the best way to debug this is to figure out what value of i causes a 'NoneType'. So I tried to set a conditional breakpoint where stringConverted = 'NoneType' so that when I run it, the program should stop when this condition is true. But when I try to run it after setting the conditional break point, it just runs all the way through producing the same error. what am I doing wrong here? Is the way I formatted my conditional breakpoint statement correct?

def main():
    totalChars = 0
    for i in range(1,500):
        stringConverted = stringConvert(i)
        totalChars = totalChars + len(stringConverted)
    print totalChars
هل كانت مفيدة؟

المحلول

def main():
    totalChars = 0
    for i in range(1,500):
        stringConverted = stringConvert(i)
        if stringConverted is None:
            print i
            break
        totalChars = totalChars + len(stringConverted)
    else:
        print "No error!"
    print totalChars

نصائح أخرى

You should use if stringConverted is None instead of checking the type to see if it is NoneType.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top