Reputation:
Okay, so first off, let me share my code and then I will ask my question. This is the code I have so far:
restaurant_dictionary = {2: ['a','A','b','B','c','C'],
3: ['d','D','e','E','f','F'],
4: ['g','G','h','H','i','I'],
5: ['j','J','k','K','l','L'],
6: ['m','M','n','N','o','O'],
7: ['p','P','q','Q','r','R','s','S'],
8: ['t','T','u','U','v','V'],
9: ['w','W','x','X','y','Y','z','Z']}
while key,value not in restaurant_dictionary:
restaurant_number = raw_input("Please enter a phone number to translate:")
Now, my question is, I want a user to input a phone number in the
format XXX-XXX-XXXX, and I want to first print, on the screen for the
user to see, "Please enter a phone number to translate:"
. Then, I want
to let them input 10 letters or digits for the phone number, based on
the restaurant_dictionary
I created above. I want to print the final
phone number they input with dashes in between the first three and
second three letters/digits and in between the second three and last
four letters/digits.
Then, based on my restaurant dictionary above, it
should be an easy matter of translating their input into only the
digits that I use as keys for my values above. I started a while
loop
above, as you can see, because I figured that if they don't give valid
input that corresponds to my dictionary, I should give them the
opportunity to reenter the letter/digit.
Oh, and here is a sample of how the program should function when it is complete:
Enter a phone number to be translated:555-GET-FOOD↵
555-438-3663↵
Enter a phone number to be translated:abc-flo-wers↵
222-356-9377↵
Upvotes: 0
Views: 388
Reputation: 123473
All you need is a reverse dictionary, which you could hardcode or create on-the-fly from a version the dictionary you have in your question with the uppercase letters removed (since there's no need to have both).
Here's a demo:
restaurant_dictionary = {2: ['a','b','c'],
3: ['d','e','f'],
4: ['g','h','i'],
5: ['j','k','l'],
6: ['m','n','o'],
7: ['p','q','r','s'],
8: ['t','u','v'],
9: ['w','x','y','z']}
# Construct a reverse mapping (i.e. letters to number)
letters = [letter for letters in restaurant_dictionary.values() for letter in letters]
rev_dict = {}
for letter in letters:
for d, chars in restaurant_dictionary.items():
if letter in chars:
rev_dict[letter] = str(d)
break
def convert(phone_num):
return ''.join(rev_dict.get(char, char) for char in phone_num.lower())
#phone_num = list(input('Please enter your phone number: ')
for phone_num in ('555-GET-FOOD', 'abc-flo-wers', 'PYT-HON-TEST'):
print(f'{phone_num} -> {convert(phone_num)}')
Output:
555-GET-FOOD -> 555-438-3663
abc-flo-wers -> 222-356-9377
PYT-HON-TEST -> 798-466-8378
Upvotes: 1
Reputation: 6156
Does this solve the issue:
restaurant_dictionary = {('a', 'b', 'c'): 2,
('d', 'e', 'f'): 3,
('g', 'h', 'i'): 4,
('j', 'k', 'l'): 5,
('m', 'n', 'o'): 6,
('p', 'q', 'r', 's'): 7,
('t', 'u', 'v'): 8,
('w', 'x', 'y', 'z'): 9}
phone_nr = list(input('Please enter Your phone number: ').lower().replace('-', ''))
temp_lst = list()
for char in phone_nr:
if char.isdigit():
temp_lst.append(char)
continue
for key, item in restaurant_dictionary.items():
if char in key:
temp_lst.append(str(item))
break
phone_nr = ''.join(temp_lst)
print(f'{phone_nr[:3]}-{phone_nr[3:6]}-{phone_nr[6:11]}')
to mention, at least in this case there was no need to include both capital and lowercase letters in the dictionary since the user input can be lowered altogether and then You could eval the whole thing against lowercase letters. it saves some space and time
Upvotes: 1