I am trying to create a function in which a given value (passed as a string) is checked to find out if the number of digits is 4 or 6, and what is the number.
My first impulse was to go with this code:
def number(x):
if (len(x) == (4 or 6)) and x.isdigit():
print "True"
else:
print "False"
This code above only passes the first test below ... I donβt understand why it passes this, but none of the other tests:
number("1234")
Only when I separate the len () functions will it work correctly.
def number(x):
if (len(x) == 4 or len(x) == 6) and x.isdigit():
print "True"
else:
print "False"
number("1234")
number("123456")
number("abcd")
number("abcdef")
number("1")
number("a")
The above code passes all the tests.
So my questions are:
- What's going on here?
- Any way to write cleaner code for this?
Thanks for the help!
** , , , - len(), isdigit() , (- ). .