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:

Untitled 24.jpg

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")

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo