CBSE Class 12 Computer Science Question 94 of 105

Python Revision Tour II — Question 1

Back to all questions
1
Question

Question 1

Write a program that prompts for a phone number of 10 digits and two dashes, with dashes after the area code and the next three numbers. For example, 017-555-1212 is a legal input.
Display if the phone number entered is valid format or not and display if the phone number is valid or not (i.e., contains just the digits and dash at specific places).

Solution
phNo = input("Enter the phone number: ")
length = len(phNo)
if length == 12 \
    and phNo[3] == "-" \
    and phNo[7] == "-" \
    and phNo[:3].isdigit() \
    and phNo[4:7].isdigit() \
    and phNo[8:].isdigit() :
    print("Valid Phone Number")
else :
    print("Invalid Phone Number")
Output
Enter the phone number: 017-555-1212
Valid Phone Number

=====================================

Enter the phone number: 017-5A5-1212
Invalid Phone Number
Answer