Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enumerating over a input

Basically so my objective for doing this was, to make it easier for writing an if statement after enumerate to see if the input string contains any numbers in the middle of the string.. if it does than to return True so that it is invalid… which could be a couple issues with doing this in the main function but hopefully someone can let me know if I am going about this a "smart" way, and how to actually implement it correctly into the main function.

import string


def main():
    plate = input("Plate: ")
    return_val = exclusions(plate)
    checkplate = platecheck(plate)

    if return_val:
        print("Invalid")
    else:
        print(checkplate)
        print("Valid")


def exclusions(s):
    new_string = s.translate(
        str.maketrans('', '', string.punctuation))
    if len(new_string) < len(s):
        print("Please do not use punctuation")
        return True
    elif 2 <= len(new_string) <= 6:
        return False
    else:
        return True


def platecheck(s):
    for i, l in (enumerate(s, 1)): 
        print(l, i) # Some sort of if statement here


main()

Everything currently works as far as the the exclusions function, making sure there is no punctuation, and making sure the input is between 2 to 6 characters.

Example of input/output if working correctly:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Input:

t33st

Output:

Numbers only allowed at the end of the plate
Invalid

Current Output:

t 1
3 2
3 3
s 4
t 5
None
Valid

>Solution :

One simple way to determine if the tail is digits is to find the first digit and call isdigit on the remainder of the string:

def platecheck(s):
    first = next(i for i, c in enumerate(s), len(s))
    return not s[first:].isdigit()

A more general tool for checking text patterns is called regular expressions, which python implements via the re module:

plate_pattern = re.compile('\D*\d*')
def platecheck(s):
    return not plate_pattern.fullmatch(s)

You can write exclusions more elegantly as

def exclusions(s):
    if any(c in string.punctuation for c in s):
        print("Please do not use punctuation")
        return True
    return len(s) > 6 or len(s) < 2

Since True indicates a problem, you should combine your functions with or:

return_val = exclusions(plate) or platecheck(plate)

More generally, if you have a list of functions, you can use any, which short-circuits just like or:

tests = [exclusions, platecheck]
invalid = any(test(plate) for test in tests)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading