Search code examples
pythondictionaryinputformattingkey

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?


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↵

Solution

  • 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