Question

Write a function called phoneNumberFormat that takes a string containing a ten-digit phone number (such as 5155551212) as input, convert into a more readable string with parentheses and dashes like (515)555-1212 and display it. If the input string contains more than or less than ten characters, display an error message. Note: Steps of phoneNumberFormat function can be given as follows: def phoneNumberFormat (phoneNum): 1. Let l be the length of the phoneNum 2. If 1 does not equal 10 then, display an error message. 3. Otherwise, . areaCodefirst three characters of phobeNum. ii. next3digits the substring of the next three digits of phoneNum phoneNum areaCode, close parenthesis, next3digits, dash, last4digits last4digits = the substring of the last four digits of iv. Concatenate the substrings in the order, open parenthesis, and display the result. For example, phoneNumberFormat (5155551212) displays the string (515) 555- 1212 and phoneNumberFormat ( 515555) displays an error message. Save the seript as lab4_checkpoint1.py in the cs104lab4 folder.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def phoneNumberFormat(phoneNum):
    if len(phoneNum) != 10:
        print('Error. Length of phone number is not 10.')
    else:
        areaCode = phoneNum[:3]
        next3digits = phoneNum[3:6]
        last4digits = phoneNum[6:]
        result = '(' + areaCode + ')' + next3digits + '-' + last4digits
        print(result)


phoneNumberFormat('5155551212')

(515) 555-1212 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Write a function called "phoneNumberFormat" that takes a string containing a ten-digit phone number (such as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Numbers with embedded dashes play a huge role in our lives. Think of your phone number...

    Numbers with embedded dashes play a huge role in our lives. Think of your phone number or Social Security number. Write a program that uses a Scanner to take a single line of user input. You may assume this input consists of any number of characters and contains zero, one or two dashes. Use only String's indexOf, lastIndexOf, and substring methods. Sample input might be 123-45-6789 or 919-555-1212 or hare-brained Your program should remove the dashes and print the digits....

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • Use c-strings for the following project: Write a C++ program that declares an array containing up...

    Use c-strings for the following project: Write a C++ program that declares an array containing up to a maximum of 20 sentences, each sentence of maximum 81 characters long, using c-strings. Continue reading sentences from the user and store them in the array of sentences, until the user enters a NULL string for the sentence or 20 sentences have been entered. Then, one by one, display each sentence entered by the user and present the following menu of operations on...

  • I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks....

    I NEED SAMPLE PRINT OUT AS WELL AS CODE PLEASE!!!! Objectives: To gain experience with stacks. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5% Explain the purpose of each class you develop in the program. - 5%. Programming:...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT