Hello im doing a home assignment. Write a program that asks the user to enter a 12-character telephone number in the format: XXX-XXX-XXXX. Acceptable characters (X's) are A-Z and a-z. Your program should check for:
The length of the phone number is correct. The dashes are included and are in the correct positions. There are no characters in the illegal characters in the string.
The application should display the telephone number with any alphabetic characters that appeared in the original translated to their numeric equivalent. If the input string is not entirely correct then you should print an error message.
My code is as follows
# asks the user to enter their phone number string
phoneNum = input("Input number: ")
# creates a new list to be appended with our future values
new_phone_num = []
# main operation to convert the letters to a
# real phone number
for i in phoneNum:
if i == 'A' or i == 'B' or i == 'C':
i == '2'
elif i == 'D' or i == 'E' or i == 'F':
i = '3'
elif i == 'G' or i == 'H' or i == 'I':
i = '4'
elif i == 'J' or i == 'K' or i == 'L':
i = '5'
elif i == 'M' or i == 'N' or i == 'O':
i = '6'
elif i == 'P' or i == 'Q' or i == 'R' or i == 'S':
i = '7'
elif i == 'T' or i == 'U' or i == 'V':
i = '8'
elif i == 'W' or i == 'X' or i == 'Y' or i == 'Z':
i = '9'
new_phone_num += i
# print statement to show the phone number
print('\nNew number: ', end='')
for r in new_phone_num:
print(r,end='')
print()
my issue is the program will not do operations on the first part of the string. for example
Input number: ABC-DEF-GHIJ
New number: ABC-333-4445
I cannot figure out how to change the first part of the string to its numeric value.
You have an equal sign too much in i == '2'
, which results in a comparison instead of an assignment. Aside from that, you can make your life easier by using a dictionary with two list comprehensions for the substitution and a string instead of a list for the new phone number:
# asks the user to enter their phone number string
phoneNum = input("Input number: ").upper() # in case the user enters lowercase letters
# creates a new list to be appended with our future values
new_phone_num = ""
sub_dict = {k: v for k, v in zip("ABCDEFGHIJKLMNOPQRSTUVXYZ", [str(n // 3) for n in range(6, 30)])}
# sub_dict will be {'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': '8', 'T': '8', 'U': '8', 'V': '9', 'X': '9', 'Y': '9'}
# main operation to convert the letters to a
# real phone number
for i in phoneNum:
new_phone_number += sub_dict[i]
# print statement to show the phone number
print('New number: %' % new_phone_number)
The expanded equivalent of
sub_dict = {k: v for k, v in zip("ABCDEFGHIJKLMNOPQRSTUVXYZ", [str(n // 3) for n in range(6, 30)])}
would be
numbers = []
for n in range(6, 30):
numbers.append(n // 3)
sub_dict = {}
for key, value in zip("ABCDEFGHIJKLMNOPQRSTUVXYZ", numbers):
sub_dict[key] = value