Here is an exercise: Define a function postalValidate(S) which first checks if S represents a postal code which is valid:
- first, delete all spaces;
- the remainder must be of the form L#L#L# where L are letters (in either lower or upper case) and # are numbers.
- If S is not a valid postal code, return the boolean False.
- If S is valid, return a version of the same postal code in the nice format L#L#L# where each L is capital.
And here is one of the numerous trials:
def postalValidate(S):
for x in S:
if x == " ":
S = S.remove(x)
if x.isalpha():
x = x.upper()
if x.isalpha() or ix.isdigit():
if ((S.index(x) % 2 == 0) and x.isalpha()) or ((S.index(x) % 2 != 0) and x.isdigit()):
pass
else:
print(False)
break
return(False)
else:
print(False)
break
return(False)
elif x.isdigit:
pass
else:
print(False)
break
return(False)
elif x.isalpha():
if S.index(x) % 2:
pass
else:
break
return(False)
elif x.isdigit():
if S.index(x) % 2 == 1:
pass
else:
break
return(False)
else:
break
return(False)
pass
print(S)
return(S)
What is my error?