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

Always return false in python outright, it is true

I am writing a program in python that take some string and test it through some condition

  1. the first 2 characters in the sting must be letters

2 . the steer must be maximum of 6 characters and at least 2 characters

3 . Numbers cannot be used in the middle of the string; they must come at the end. For example, AAA222 would be an acceptable … ; AAA22A would not be acceptable

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

4 . The first number used cannot be a ‘0’.”

5 . [‘ ‘, ‘,’, ‘;’, ‘-‘, ‘_’] thos characters are not allowed

this is my code so far

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


def is_valid(s):
    total = is_N_1(s) + is_N_2(s) + is_N_3(s) + is_N_4(s) + is_N_5(s)
    if total  == 5:
        #print (total)
        return True
    else:
        #print(total)
        return False


def is_N_1(s):
    if len(s)<7 and len(s)>3:
        return 1
    else:
        return 0

def is_N_2(s):
    if s[0:2].isalpha():
        return 1
    else:
        return 0

def is_N_3(s):
    for i in s:
        if s[-1].isalpha() and i.isnumeric():
            return 0
        else:
            return 1

def is_N_4(s):
    t = []
    for i in s:
        if i.isdigit():
            t.append(i)

    if len(t)<=0:
        return 1
    else:
        if t[0] == '0':
            return 0
        else:
            return 1

def is_N_5(s):
    not_allow =[' ', ',', ';', '-', '_']
    for i in s :
        for _ in not_allow :
            if i == _:
                return 1
            else :
                return 0
main()

this is the input that give an error in the output

1 . input of "CS50" 
    expected "Valid", not "Invalid\n"

2 . input of "ECTO88"
    expected "Valid", not "Invalid\n"

3 . input of "NRVOUS"
    expected "Valid", not "Invalid\n"

can any one take a look at this and tell me what I did wrong, I stack for 2 hours and I did not find the solution?

>Solution :

Below is how to check the string in s one by one for not allowed characters within not_allow. I think that just fixing this part will solve it.

def is_N_5(s):
    not_allow =[' ', ',', ';', '-', '_']
    for i in s :
        if i in not_allow:
            return 0
    return 1
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