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)

Dani AI

Generated

Two quick issues in the original post that explain why the branches never behave as expected: assigning A = 2 etc. makes A a number, not the letter "A", and an expression like if UserInput == A or B or C: is evaluated as (UserInput == A) or B or C — since B/C are nonzero integers that is truthy, the if always succeeds. Use string values and membership checks, or better yet use a single lookup table.

A compact, reliable approach that works in Python 3.2 is to build a letter→digit mapping once and then look up the cleaned input. This example zips the alphabet against the standard phone digits and validates the input:

import string

digit_map = dict(zip(string.ascii_uppercase,
                     "22233344455566677778889999"))

letter = input("Enter a single letter: ").strip().upper()
if len(letter) == 1 and letter.isalpha():
    print("The digit for {} is {}".format(letter, digit_map[letter]))
else:
    print("Please enter exactly one alphabetical character (A-Z).")

That keeps the mapping data separate from control flow (easier to test and reuse). To convert whole words or phonewords, map each letter and join the results, using digit_map.get(ch, '?') to show non-letters distinctly.

Notes: ’s dictionary idea and ’s grouping technique are both valid — this solution just creates the dictionary automatically from a compact pattern. Always normalize input with .strip() and .upper() and validate isalpha() to avoid surprises.

Recommended Answers

All 2 Replies

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.