Python Forum
checking a postal code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
checking a postal code
#1
I know this is something so simple that I'm missing but I can't figure it out. I'm in first year Python and we've been tasked with setting up a hypothetical student database. One of the requests is for the student to enter their postal code. Our program is to check that it's 6 digits long and that its letter/digit/letter/digit/letter/digit. There's no exception raised, but it always says Congrats even if I put in all digits.

Thanks for your help -

Jude

Here is my code:

def main():

    valid=True
    while valid:
        PostalCode=input("Enter postal code, no spaces: ")
    
        if checkCode(PostalCode):
            valid=False
        else:
            print("Error, please try again. 6 digits, no spaces ")

    print(PostalCode, "Congrats, you solved it.")


def checkCode(spost):

    codelength=len(spost)
    if codelength==6 and spost[0:5:2].isdigit and spost[1:5:2].isalpha:
        return True
    else:
        return False


main()
Reply
#2
Isn't Canadian postal code 7 digits: 'A1A 1A1' ?
You can do it with a format string:
def main():
    valid = False
    while not valid:
        PostalCode = input("Enter postal code, no spaces: ")

        valid = checkCode(PostalCode)
        if not valid:
            print("Error, please try again. 6 digits, no spaces ")

    print(PostalCode, "Congrats, you solved it.")


def checkCode(spost):
    isgood = True
    codelength = len(spost)
    if codelength == 7:
        pformat = '101 010'  # 0=digit, 1=alpha, space=space
        for n, char in enumerate(spost):
            if pformat[n] == '0':
                isgood = char.isdigit()
            elif pformat[n] == '1':
                isgood = char.isalpha()
            else:
                isgood = char.isspace()
            if not isgood:
                break
    else:
        isgood = False
    return isgood
main()
what this does is check each character in string against the format list
isdecimal, isalpha, isspace, depending on whether that character position should hold a digit, character or space.
Reply
#3
Thanks so much for your time, Larz. Unfortunately, we have to use functions we have learned in our book and enumerate is not one of them. The idea of setting isgood as a boolean is cool
Reply
#4
without enumerate:
def checkCode(spost):
    isgood = True
    codelength = len(spost)
    if codelength == 7:
        pformat = '101 010'  # 0=digit, 1=alpha, 2 = space
        n = 0
        for char in spost:
            if pformat[n] == '0':
                isgood = char.isdigit()
            elif pformat[n] == '1':
                isgood = char.isalpha()
            else:
                isgood = char.isspace()
            if not isgood:
                break
            n += 1
    else:
        isgood = False
    return isgood
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020