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

Why is my function always outputting "invalid" as a result?

I am learning python. In this context, I do an exercise in which I need to perform a series of test on a string. If these checks are met, python should output "Valid", if not "Invalid".

Here is my code, with the comments describing the goal of each functions:

import string

def main():
    plate = input("Plate: ")
    if is_valid(plate):
        print("Valid")
    else:
        print("Invalid")

# Function "main" calls the "is_valid" function

def is_valid(s):
    if lenght_valid(s) == True and starts_alpha(s) == True and check_ponctuation(s) == False:
        return True
    else:
        return False

# Function "is_valid" calls all the other function checking single cirterias

def lenght_valid(s):
    if len(s) > 6 or len(s) < 2:
       return False
    else:
       return True

# Function "lengt_valid" check the propoer lenght (2 characters min., 6 max.)


def starts_alpha(s):
    s = s[0:2]
    if s.isalpha():
       return True
    else:
       return False

# Function "starts_alpha" check that the first two characters are letters

def check_ponctuation(s):
    return (character in string.punctuation for character in s)

# Function "check_punctuation" checks if punctuation is present. If "True" then this will be rejected in the above function

main()

I do not understand why it always outputs "Invalid". I tested "KA" and "KOKO" and these should be valid, however, these are not valid.

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

>Solution :

Your function, check_ponctuation is returning a generator, not a boolean result.

def check_ponctuation(s):
    return (character in string.punctuation for character in s)

This function is not doing what you think it is doing.

Presumably, you’ve missed an any() call.

This code is what it should be:

def check_ponctuation(s):
    return any(character in string.punctuation for character in s)

The reason that any is needed, is that if you call

return (character in string.punctuation for character in s)

by itself, you’re returning a generator object which could result in undefined behavior.

Instead, by using any(), you can check to see if your string contains any punctuation and return a true or a false based on that.

Here’s some information about any() in python and how it works:

"The any() function returns True if any item in an iterable are true, otherwise it returns False.

If the iterable object is empty, the any() function will return False."

https://www.w3schools.com/python/ref_func_any.asp

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