如何让用户根据我创建的字典输入 phone 数字,并让他们以 XXX-XXX-XXXX 格式输入键或值?
How to get user to input phone number based on dictionary I created and get them to either enter key or value in format XXX-XXX-XXXX?
好的,首先,让我分享我的代码,然后我会问我的
问题。这是我到目前为止的代码:
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:")
现在,我的问题是,我希望用户在
格式XXX-XXX-XXXX,我想先打印,在屏幕上为
用户看到,"Please enter a phone number to translate:"
。那么,我要
让他们为 phone 号码输入 10 个字母或数字,基于
restaurant_dictionary
我在上面创建的。我想打印决赛
phone 他们输入的数字,前三个和中间有破折号
第二个三个 letters/digits 在第二个和最后一个之间
四个 letters/digits.
然后,根据我上面的餐馆词典,它
将他们的输入仅翻译成
我用作上述值的键的数字。我开始了一个 while
循环
上面,正如你所看到的,因为我认为如果他们不提供有效的
输入对应于我的字典,我应该给他们
有机会重新进入 letter/digit.
哦,这里是程序运行时应该如何运行的示例
完成:
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↵
这是否解决了问题:
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]}')
值得一提的是,至少在这种情况下,不需要在字典中同时包含大写字母和小写字母,因为用户输入可以完全降低,然后您可以根据小写字母评估整个内容。它节省了一些 space 和时间
您所需要的只是一个反向字典,您可以对它进行硬编码或从您问题中删除大写字母的字典版本中即时创建(因为不需要同时拥有这两个字典)。
这是一个演示:
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)}')
输出:
555-GET-FOOD -> 555-438-3663
abc-flo-wers -> 222-356-9377
PYT-HON-TEST -> 798-466-8378
好的,首先,让我分享我的代码,然后我会问我的 问题。这是我到目前为止的代码:
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:")
现在,我的问题是,我希望用户在
格式XXX-XXX-XXXX,我想先打印,在屏幕上为
用户看到,"Please enter a phone number to translate:"
。那么,我要
让他们为 phone 号码输入 10 个字母或数字,基于
restaurant_dictionary
我在上面创建的。我想打印决赛
phone 他们输入的数字,前三个和中间有破折号
第二个三个 letters/digits 在第二个和最后一个之间
四个 letters/digits.
然后,根据我上面的餐馆词典,它
将他们的输入仅翻译成
我用作上述值的键的数字。我开始了一个 while
循环
上面,正如你所看到的,因为我认为如果他们不提供有效的
输入对应于我的字典,我应该给他们
有机会重新进入 letter/digit.
哦,这里是程序运行时应该如何运行的示例 完成:
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↵
这是否解决了问题:
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]}')
值得一提的是,至少在这种情况下,不需要在字典中同时包含大写字母和小写字母,因为用户输入可以完全降低,然后您可以根据小写字母评估整个内容。它节省了一些 space 和时间
您所需要的只是一个反向字典,您可以对它进行硬编码或从您问题中删除大写字母的字典版本中即时创建(因为不需要同时拥有这两个字典)。
这是一个演示:
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)}')
输出:
555-GET-FOOD -> 555-438-3663
abc-flo-wers -> 222-356-9377
PYT-HON-TEST -> 798-466-8378