I know this code does not look good and I want to overhaul it anyway, but can somebody please explain to me why this happens:
If I give “aa1f” as input it gives back “Invalid”, as it should,
but if I give “aa1a” as input I get “Valid” as Output.
I’m really confused why this happens and want to understand it before I rewrite it.
Thank you all in advance!
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
#plate must be longer than 2 symbols
if len(s) < 2:
return False
#first 2 symbols must be letters
for i in range(2):
if s[i].isalpha() == False:
return False
#plate must >=2 and <=6 symbols long
if 2 <= len(s) <= 6:
pass
else:
return False
#all symbols must be either numbers or letters
for i in range(len(s)):
if s[i].isnumeric() == False and s[i].isalpha() == False:
return False
#
first_num = len(s) - 1
for i in s:
if i.isnumeric():
if i == "0":
return False
first_num = s.index(i)
break
for character in s:
if s.index(character) <= first_num:
pass
else:
if character.isalpha():
return False
return True
if __name__ == "__main__":
main()
New contributor
Hendrik Chew is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1