Python Forum

Full Version: Help me out?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need help creating a boolean function that will return True if the input has an odd number of digits. No luck so far. Anyone know how to do it, without using a while loop, or advanced python functions? Huh
We are glad to help, but we are not going to do your homework. Please, show us what have you tried so far. Please, use proper tags when post code, traceback, output, etc. See BBcode help for more info.
I don't really have an idea of what to do. I am currently testing numbers with the floor division and modulus functions. I have found some similar ideas online, but all contained the while loop which I do not have knowledge of. From what I have been trying out, I think the solution to this function has something to do with the floor division function, but I haven't been able to find solid proof.
hint to start: assuming function takes an int as argument, convert it to str and find the length of that str.
Thanks very much for that hint! Helped me a lot.
def odd_number_digits(n):
    if (len(str(n)) % 2 != 0
        return True
This is what I have so far, however, I keep getting a syntax error message referring to my return line. Is there a visible syntax issue?
check the number of closing brackets on the previous line or just remove the most-left opening bracket - it's redundant

Also the function may be simplified to
def odd_number_digits(n):
    return len(str(n)) % 2 != 0
    # return bool(len(str(n)) % 2)

if __name__ == '__main__':
    print(odd_number_digits(123))
    print(odd_number_digits(12))