Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # def main():
- # plate = input("Plate: ")
- # if is_valid(plate):
- # print("Valid")
- # else:
- # print("Invalid")
- def is_valid(s):
- return (
- check_digits_after_digit(s)
- and text_length(s)
- and punctuation_check(s)
- and two_letters(s)
- and zero_first_digit(s)
- )
- # check if the first letters are alpha
- def two_letters(text):
- return text[:2].isalpha()
- # check the length of the string
- def text_length(text):
- return 2 <= len(text) <= 6
- # check if there is a number in the plate and if it the first one is zero
- def zero_first_digit(text):
- for c in text:
- if c.isdigit() and int(c) != 0: # check if there is a number
- print("good to go 0 not first digit")
- return True
- else:
- if c.isdigit() and int(c) == 0:
- print("first digit is zero")
- return False
- # Check if the characters following a digit to the end of the string, are digits
- def check_digits_after_digit(text): # if everything is alpha
- print("test alpha / numbers check is running")
- if text.isalpha():
- print("all chars are ALPHA")
- return True
- else:
- found_digit = False # initialize var to store location of first digit and set it to False
- for c in text:
- if c.isdigit():
- found_digit = True # found first digit set flag to true
- idx = text.index(c) # get the index location of the digit
- break
- if found_digit:
- print("index is", idx)
- if text[idx:].isdigit(): # if all the rest chars are digit
- print("all the rest are digits")
- return True
- else: # if the rest are not digit
- print("not all digits")
- return False
- # check if there is punctuation in text
- def punctuation_check(text):
- for c in text:
- if c in "'.,;:?! ":
- print("punctuation fault")
- return False
- else:
- return True
- # if __name__ == "__main__":
- # main()
- test = ["CS50", "ECTO88", "NRVOUS", "CS05", "CS50P2", "PI3.14", "H", "OUTATIME", "CS50P", "HELLO"]
- for test_case in test:
- print("Considering", test_case)
- if is_valid(test_case):
- print("The outcome of", test_case, "is Valid")
- print("----")
- else:
- print("The outcome of", test_case, "is Invalid")
- print("----")
Advertisement
Add Comment
Please, Sign In to add comment