Im trying to write a program that inputs a single letter and prints out the corresponding digit on the telephone. Im having trouble finding out a way in which you can group the variables that have the same digit, but im not sure how. You guys have any suggestions? P.S. im running python 3.2.2

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

digit = ""

UserInput = str(input("Enter a single letter, and I will tell you what the correrponding digit on the telephone:"))

if UserInput == A or B or C:
    digit = "2"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")

elif UserInput == D or E or F:
    digit = "3"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")

elif UserInput == G or H or I:
    digit = "4"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")
    
elif UserInput == J or K or L:
    digit = "5"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")

elif UserInput == M or N or O:
    digit = "6"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")

elif UserInput == P or Q or R or S:
    digit = "7"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")

elif UserInput == T or U or V:
    digit = "8"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")

elif UserInput == W or X or Y or Z:
    digit = "9"
    print("The digit",digit,"corresponds to the letter", UserInput, "on the telephone.")

else :
    print("There is no digit on the telephone that corresponds to",UserInput)

A dictionary can ofen short down long if/elif statement.
Something like this.

def find_numb(phone_dict, user_input):
    try:
        return [k for k, v in phone_dict.items() if user_input in v][0]
    except IndexError:
        return('Not found')

user_input = input("Enter a single letter: ") #Return a string
phone_dict = {2: ('a', 'b', 'c'), 3: ('d', 'e', 'f')}
print(find_numb(phone_dict, user_input))

You can also use a list of lists

letters_to_numbers= [['ABC', 2], ['DEF', 3]]
for test_it in ["A", "E", "C"]:
    print
    for group in letters_to_numbers:
        print "testing %s in %s" % (test_it, group[0])
        if test_it in group[0]:
            print "     the number for %s is %d" % (test_it, group[1])
#
# or match the letters and the element number of a list
#
print "-"*50
letters_to_numbers= ['*', '*', 'ABC', 'DEF']
for test_it in ["A", "E", "C"]:
    print
    for num, group in enumerate(letters_to_numbers):
        print "testing %s in %s" % (test_it, group)
        if test_it in group:
            print "     the number for %s is %d" % (test_it, num)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.