Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me out?
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
hint to start: assuming function takes an int as argument, convert it to str and find the length of that str.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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?
Reply
#6
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))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020