I am writing a program in python that take some string and test it through some condition
- 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
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?
CodePudding user response:
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
CodePudding user response:
In the 5th condition, you should reverse the return values. Now you return 1 when one of not allowed chars is used.
def is_N_5(s):
not_allowed = [' ', ',', ';', '-', '_']
for i in s:
if i in not_allowed:
return 0
return 1
CodePudding user response:
Your function is_N_5 has an invalid reference. not_allwo should be updated to not_allow
def is_N_5(s):
not_allow =[' ', ',', ';', '-', '_']
for i in s :
for _ in not_allow :
if i == _:
return 1
else :
return 0