Write a program that prompts for a phone number of 10 digits and two dashes after the area code and the next three digits of the number. (eg. 017-230-0037). Display if the phone number entered is in valid format or not and also of the phone number is valid or not.

 

Answer:

[Strings] Write a program that prompts for a phone number of 10 digits - Long Answer Type Questions (5 Marks each)

Code:

phone_no=input( "Enter phone number: " )

flag= 0

for i in range(len(phone_no)):

    if i==3 or i==7:

 if phone_no[i]!= '-' :

            print( "Invalid phone number" )

            flag= -1

            break

    else:

        if phone_no[i].isalpha():

            print( "Invalid phone number" )

            flag= -1

            break

if flag== 0 :

    print("Valid phone number")

Remove Ads
Teachoo · Class 11 Explore Class 11
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.