Jan-03-2018, 11:52 PM
I'm having trouble with an error in my code and I cannot find out why it is happening.
This is the task:
Most non-smartphone mobile phones have keypads like this:
Given a word of input, all in uppercase letters, print the numbers you would need to type on a standard mobile phone keypad to enter that word. Assume that the phone can perfectly predict the word you want, and there are no numbers or punctuation symbols in the word.
For example:
Enter word: GROK
4765
4765 is printed out since GROK is entered by pressing 4, 7, 6, and 5.
This is what I have done so far:
This is the task:
Most non-smartphone mobile phones have keypads like this:
Given a word of input, all in uppercase letters, print the numbers you would need to type on a standard mobile phone keypad to enter that word. Assume that the phone can perfectly predict the word you want, and there are no numbers or punctuation symbols in the word.
For example:
Enter word: GROK
4765
4765 is printed out since GROK is entered by pressing 4, 7, 6, and 5.
This is what I have done so far:
i=0 out=[] KEYPAD = { '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': '7', 'T': '8', 'U': '8', 'V': '8', 'W': '9', 'X': '9', 'Y': '9', 'Z': '9', } word=input("Enter word: ") words=[ch for ch in word] while len(word)>=i: if words[i] in KEYPAD: x=KEYPAD[i] out.append(x) i=i+1 else: i=i+1 print(out)
Output:Enter word: GROK
Traceback (most recent call last):
File "program.py", line 15, in <module>
x=KEYPAD[i]
KeyError: 0