Question

In: Computer Science

PYTHON: Write a program that asks the user to enter a 10-character telephone number in the...

PYTHON:

Write a program that asks the user to enter a 10-character telephone number in the format XXX-XXX-XXXX. The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. For example, if the user enters 555-GET-FOOD, the application should display 555-438-3663.

This is my code, but I cannot figure out where to go from here.

#set new number
new_number = ""
#split number
split_num = phone.split("-")
for char in split_num[1:2]:
for x in "ABC":
if x == "A" or x == "B" or x == "C":
num = "2"
elif x == "D" or x == "E" or x == "F":
num = "3"
elif x== "G" or x == "H" or x == "I":
num = "4"
elif x == "J" or x == "K" or x == "L":
num = "5"
elif x == "M" or x == "N" or x == "O":
num = "6"
elif x == "P" or x == "Q" or x == "R" or x == "S":
num = "7"
elif x == "T" or x == "U" or x == "V":
num = "8"
elif x == "W" or x == "X" or x == "Y" or x == "Z":
num = "9"
else:
num = x
new_number += num
print(new_number)

Solutions

Expert Solution

Here is the solution to the above problem:

#ask the user for the input
number = input("Enter the number: ")
#split it using the split method
actualNum = number.split("-")
finalNum =""

#now the actualNum is an array of string, we need to handel each separetely
for chunk in actualNum:
    for i in range(0,len(chunk)):
        #thanks to you we can we this logic directly from the question provided
        x = chunk[i]
        if x == "A" or x == "B" or x == "C":
            finalNum = finalNum + "2"
        elif x == "D" or x == "E" or x == "F":
            finalNum = finalNum +  "3"
        elif x== "G" or x == "H" or x == "I":
            finalNum = finalNum +  "4"
        elif x == "J" or x == "K" or x == "L":
            finalNum = finalNum +  "5"
        elif x == "M" or x == "N" or x == "O":
            finalNum = finalNum +  "6"
        elif x == "P" or x == "Q" or x == "R" or x == "S":
            finalNum = finalNum +  "7"
        elif x == "T" or x == "U" or x == "V":
            finalNum = finalNum +  "8"
        elif x == "W" or x == "X" or x == "Y" or x == "Z":
            finalNum = finalNum +  "9"
        else:
            finalNum = finalNum + x
    finalNum = finalNum + "-"

#this is to remvoe the last character from the string which is -
finalNum = finalNum[:-1]
print(finalNum)

Here is the output of the above code:


Related Solutions

In java, write a program that asks the user to enter a character. Then pass the...
In java, write a program that asks the user to enter a character. Then pass the character to the following methods. isVowel() – returns true if the character is a vowel isConsonant() – returns true if the character is a consonant changeCase() – if the character is lower case then change it to upper case and if the character is in upper case then change it to lower case. (return type: char) Example output is given below: Enter a character...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Python Programming Exercise Scenario: Write a program that asks the user to enter scores the number...
Python Programming Exercise Scenario: Write a program that asks the user to enter scores the number is based on what the user wants to enter. The program will display a letter grade and associated message for each score, based on the table below, and the average score. The program will not contain any repeated code and have a minimum of two functions besides Main. Score                Letter Grade             Message 90 – 100                       A                     Excellent work 89 – 80                         B                     Nice job 79 –...
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0. using for-loop
Develop a program that asks a user to enter the number 10, and then it outputs...
Develop a program that asks a user to enter the number 10, and then it outputs COUNT-DOWN from 10 to 0.
Write a program that asks the user to enter an unsigned number and read it. Then...
Write a program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6. COMMENT COMPLETE CODE PLEASE
Write a program (polygon.py) that asks the user to enter the number of sides in a...
Write a program (polygon.py) that asks the user to enter the number of sides in a regular polygon. For example, an equilateral triangle is a regular 3-sided polygon, a square is a regular 4-sided polygon, and a pentagon is a regular 5-sided polygon. If a user enters a number of sides between 3 and 25, inclusive, draw the polygon and wait for the user to click on the screen to quit the program. If the user enters a number less...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT