The following code is my code for the vanity plates problem in cs50 introduction to python course. My issue is on the "Punctuation Function". When I enter an input with a punctuation, instead of returning "valid" or "invalid", it returns this attribute error:
Traceback (most recent call last):
File "plate.py", line 55, in <module>
main()
File "plate.py", line 6, in main
if is_valid(plate):
File "plate.py", line 13, in is_valid
if number(s) and length(s) and letter(s) and punctuation(s)==True:
File "plate.py", line 44, in number
for j in range(n.start()):
AttributeError: 'NoneType' object has no attribute 'start'
Here is the code:
import re
def main():
plate = input("Plate: ")
if is_valid(plate):
print("Valid")
else:
print("Invalid")
def is_valid(s):
if number(s) and length(s) and letter(s) and punctuation(s)==True:
return True
def letter(s):
if s[0:2].isalpha():
return True
else:
return False
def length(s):
if 2 <= len(s) <= 6:
return True
else:
return False
def punctuation(s):
for i in s:
if not(i.isalpha() or i.isdigit()):
return False
return True
def number(s):
if s.isalpha():
return True
else:
n = re.search(r"\d+$", s)
for m in s:
for j in range(n.start()):
if m.isdigit():
return False
else:
return True
if n.group(0) != 0 and n:
return True
else:
return False
main()