Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 15 Online
OpenStudy (anonymous):

Medal + fan if you solve the problem: Many companies use telephone numbers such as 800-GET-REAL to make it easier for customers to remember phone numbers. On a standard telephone keypad, the letters of the alphabet are typically mapped to numbers as follows: Write a program that asks the user to enter a telephone number in the format XXX-XXX-XXXX (dashes included). Your program must eventually display the telephone number with all alphabetic characters translated into their numeric equivalents. First check to make sure the length of the input is 12 characters; if not, display an error m

OpenStudy (anonymous):

First check to make sure the length of the input is 12 characters; if not, display an error message and exit the program. If the length is correct, you can assume that the characters entered are in the proper format. As an example, if the user enters 800-GET-REAL, your program must then display 800-438-7325. As another example, if the user enters THE-123-4YOU, your program must then display 843-123-4968. As another example, if the user enters OMG-LOL-ROFL, your program must then display 664-565-7635.

OpenStudy (anonymous):

have done anything?

OpenStudy (anonymous):

No

OpenStudy (telemain):

This program will work as long as you pass the function a string with 12 characters. I didn't add any assertions for length, type, or structure, but there's a brief test brace to check that it's working for the correct type of inputs: def telephoneTranslator(number): """Translates telephone numbers that contain numbers and letters into only numbers. Must be 12 characters long, including dashes. Takes a string input of 12 digits, containing numbers, letters, and dashes only. Returns a string of 12 digits containing numbers and dashes only. """ translatorDict = {"A":2, "B":2, "C":2, "D":3, "E":3, "F":3, "G":4, "H":4, "I":4, "J":5, "K":5, "L":5, "M":6, "N":6, "O":6, "P":7, "Q":7, "R":7, "S":7, "T":8, "U":8, "V":8, "W":9, "X":9, "Y":9, "Z":9} translatedNumber = '' for digit in number: if str.isdigit(digit) or digit == '-': translatedNumber += digit else: translatedNumber += str(translatorDict[str.capitalize(digit)]) return translatedNumber def testbrace(): testsuccess = True if telephoneTranslator("800-GET-REAL") != "800-438-7325":testsuccess = False if telephoneTranslator("THE-123-4YOU") != "843-123-4968":testsuccess = False if telephoneTranslator("OMG-LOL-ROFL") != "664-565-7635":testsuccess = False if telephoneTranslator("omg-LOL-ROFL") != "664-565-7635":testsuccess = False if testsuccess == True: print "Test brace passed." else: print "Test brace failed."

OpenStudy (telemain):

By the way, here's some example assertions you could use to ensure length, structure, and content of the phone number passed to the function: assert len(number) == 12, "Wrong length" assert number[3] == '-', "Missing hyphen" assert number[7] == '-', "Missing hyphen" for digit in number: assert str.isdigit(digit) or str.isalpha(digit) or digit == '-', "Wrong format; only characters, numbers and hyphens"

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!