Codapt
Codapt

Reputation: 1

I cant find the problem in my code for translating phone numbers from alphanumeric characters to all numbers

This is my code below. When i run it in Python, if i input 1800FLOWERS, my output is 1800FLO9ERS. if i input 1800GOTJUNK, my output is 18004O85UNK. if i input 1800BIGFISH, my output is 1800BI4FISH. Its very inconsistent and i cant figure out what is wrong with my code when the output differs everytime even though my code stays the same. can anybody please help.

phonenumber = (input("Please input the number you would like to translate, starting with 1800 now, no hyphens:"))

def FindNumber(phonenumber):
    number = ""
    for char in phonenumber:
        if char == ( "A" or "B" or "C"):
            number= number + "2"
        elif char == ( "D" or "E" or "F"):
            number= number + "3"
        elif char == ( "G" or "H" or "I"):
            number= number + "4"
        elif char == ( "J" or "K" or "L"):
            number= number + "5"
        elif char == ( "M" or "N" or "O"):
            number= number + "6"
        elif char == ( "P" or "Q" or "R" or "S"):
            number= number + "7"
        elif char == ( "T" or "U" or "V"):
            number= number + "8"
        elif char == ( "W" or "X" or "Y" or "Z"):
            number= number + "9"
        else:
            number= number+char
    return number
   
print("The number you want to call is", (FindNumber(phonenumber)))

Upvotes: 0

Views: 61

Answers (3)

gimix
gimix

Reputation: 3833

The answers here correct the error in your code, so they are ok.

But this is a perfect use case for the builtin str.translate() method:

from string import ascii_uppercase
ttable = str.maketrans(ascii_uppercase, '22233344455566677778889999')
"1800FLOWERS".translate(ttable)
               
'18003569377'

Upvotes: 0

Alexander
Alexander

Reputation: 17335

It is because your conditional statements are incorrect.

the statement:

if char == ("T" or "U" or "V"):
   ...

is the same as:

temp = ("T" or "U" or "V")
if char == temp:
   ...

and since ("T" or "U" or "V") is always going to return the first character of the statement it is really only checking if char == "T"

To fix it should look like this:

if char == "T" or char == "U" or char == "V":
   ...

One possible alternative approach that could eliminate having to use so many conditional statements would be to store the values in a dictionary and compare the characters to the dictionary keys.

def FindNumber(phonenumber):
    number = ""
    seq = {"ABC": "2", "DEF": "3", "GHI": "4", "JKL": "5",
           "MNO": "6", "PQRS": "7", "TUV": "8", "WXYZ": "9"}
    for char in phonenumber:
        if char.isnumeric():
            number += char
            continue
        for key,val in seq.items():
            if char in key:
                number += val
                break
    return number

Upvotes: 1

Jim Lewis
Jim Lewis

Reputation: 45105

The or operator doesn't work the way you think it does; you can't use it as shorthand to do several comparisons at once. You could rewrite your tests like this:

    if (char == "A") or (char == "B") or (char == "C"):
        number= number + "2"

Upvotes: 0

Related Questions