Skip to content Skip to sidebar Skip to footer

Python: Input Validate With String Length

Ok so i need to ensure that a phone number length is correct. I came up with this but get a syntax error. phone = int(input('Please enter the customer's Phone Number.')) if len(str

Solution 1:

You can't have

if:else:

Because the else, being inside the first if block, doesn't have a corresponding if.

It should be:

if:
    thiselse:
    that 

Solution 2:

You may try this to be asking for the phone number until it is correct:

phone = ""whilelen(str(phone)) != 11:
    phone = int(input("Please enter the customer's Phone Number."))
phonumber.append(phone)

If you want to check also that the input is a number and not text, you should also trap the exception raised by int in that case, for example:

phone = ""whilelen(str(phone)) != 11:
    try:
        phone = int(input("Please enter the customer's Phone Number."))
    except ValueError:
        phone = ""
phonumber.append(phone)

Post a Comment for "Python: Input Validate With String Length"