I write a code to verify the license plates of cars and there are some conditions to achieve this purpose:
0
AAA12
is ok, but AA2AAA
is not.)def is_valid(s):
s = s.lower()
if len(s) >= 2 and len(s) <= 6 and s[0] != 0 and s[0:2].isalpha():
if s.isalpha():
return True
elif s.isalnum():
for sq in s.split():
for w in sq:
if w.isdigit():
x = sq.index(w)
try:
if s[w:].isalpha():
return False
else:
return True
except:
return True
So my code is doing most of these conditions in a right way, but when I enter e.g. HL23P2
, I expect to get False
but it returns True
.
Why does it return False
? because I can't have numbers between letters only it is allowed when its like e.g. HEL23
.
But how can I detect numbers between letters?
I think this solution without regular expression would be ok:
def is_valid(s):
meetNumber = False
if len(s) >= 2 and len(s) <= 6 and s[0:2].isalpha():
for i in range(2, len(s)):
if s[i].isnumeric():
meetNumber = True
elif meetNumber == True:
return False
return True
else:
return False
print(is_valid(input()))