0

Good afternoon, I being some issues trying to run this batch of code that involves converting the individual letter characters within a string into numbers. The specific letters of the alphabet are compartmentalized into distinct grouping based off of there order, as is shown within the code.

Here's what I have so far:

def main():

    gSNumberConversion()

def gSNumberConversion():

   phoneNum = input('Please Enter the number in the format of XXX-XXX-XXXX\n:')

   phoneNum = phoneNum.split('-')

   for var in phoneNum[1:2]:

       for char in var:
        if char == 'A' or char == 'B' or char == 'C':
            char = '2'
        elif char == 'D' or char == 'E' or char == 'F':
            char = '3'
        elif char == 'G' or char == 'H' or char == 'I':
            char = '4'
        elif char == 'J' or char == 'K' or char == 'L':
            char = '5'
        elif char == 'M' or char == 'N' or char == 'O':
            char = '6'
        elif char == 'P' or char == 'Q' or char == 'R' or char == 'S':
            char = '7'
        elif char == 'T' or char == 'U' or char == 'V':
            char = '8'
        elif char == 'W' or char == 'X' or char == 'Y' or char == 'Z':
            char = '9'

            print(phoneNum)

main()

The code is supposed to run through a phone number that hides a small phrase in it, such as "555-GET-FOOD", and returns its numerical equivalent. And while the input does run through, the program does not return a numerically replaced the version of the number

Rarblack
  • 4,559
  • 4
  • 22
  • 33
  • You see `phoneNum` is not touched inside the loop. So, you shouldn't be expecting it to change. – Austin Apr 03 '19 at 17:22

4 Answers4

0

First mistake is you were printing phoneNum in the inner loop, whereas you were assigning the converted digit into char variable.

Secondly phoneNum[1:2] isn't correct if you want to convert full input.

Here is the updated code -

def main():

    gSNumberConversion()

def gSNumberConversion():

   phoneNum = input('Please Enter the number in the format of XXX-XXX-XXXX\n:')

   phoneNum = phoneNum.split('-')
   print(phoneNum)

   for var in phoneNum: # mistake 2

       for char in var:
        if char == 'A' or char == 'B' or char == 'C':
            char = '2'
        elif char == 'D' or char == 'E' or char == 'F':
            char = '3'
        elif char == 'G' or char == 'H' or char == 'I':
            char = '4'
        elif char == 'J' or char == 'K' or char == 'L':
            char = '5'
        elif char == 'M' or char == 'N' or char == 'O':
            char = '6'
        elif char == 'P' or char == 'Q' or char == 'R' or char == 'S':
            char = '7'
        elif char == 'T' or char == 'U' or char == 'V':
            char = '8'
        elif char == 'W' or char == 'X' or char == 'Y' or char == 'Z':
            char = '9'

        print(char) ## mistake 1

main()
Arnab Roy
  • 619
  • 5
  • 16
  • 1
    Wow man you've actually helped out a lot in me getting a proper answer from the module. Funny to see just how far small edits could help in improving the functionality of code. my only problems now are in formatting the output to be structured like a telephone number, which I'll be working on doing now – ThePooberteen Apr 03 '19 at 17:38
0

strings are immutable, you have to create a new variable and add your results there.

newPhoneNum="" #new empty variable to add the result to
var = phoneNum[1:2] #extract the part of the phone that you will use


for index in len(var)):
        if var[index] == 'A' or var[index] == 'B' or var[index] == 'C':
            newPhoneNum+='2'
        .
        .  
        etc 
        print(phoneNum[0]+newPhoneNum)
Amany
  • 321
  • 1
  • 7
0

You can create dictionary beforehand

char_dict = { 
    '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
}

# take input
input_str = input()

# initialise result string
result = ''

# loop over input
for val in input_str:
    if val in char_dict:
        # take the number equivalent from dict
        result = result + str ( char_dict.get(val) )
    # other wise take the number or -
    else:
        result += result + val

print(result)

hope it helps.

lazarus
  • 677
  • 1
  • 13
  • 27
  • Afternoon Lazarus, yeah I'm sure that a dictionary could be used as a viable method for storing conversion information, but I feel that it would be too far derived from the initial structure I've established with my code, with has been shown to work just as well. Appreciate your input though man. – ThePooberteen Apr 03 '19 at 17:41
  • thanks @ThePooberteen , I thought dictionary will be more clean then if else ladder.. anyways you have to map certain alphabets to corresponding numbers.. – lazarus Apr 03 '19 at 17:49
0

Your print shouldn't have been placed in the loop and I used a list comprehension to reduce the loops in the program.

def main():
    gSNumberConversion()

def gSNumberConversion():

  phoneNum_inputted = input('Please Enter the number in the format of XXX-XXX-XXXX\n:')

  phoneNum = [y for x in phoneNum_inputted.split('-') for y in x]

  real_num = ""
  for char in phoneNum:
    if char == 'A' or char == 'B' or char == 'C':
        real_num += '2'
    elif char == 'D' or char == 'E' or char == 'F':
        real_num += '3'
    elif char == 'G' or char == 'H' or char == 'I':
        real_num += '4'
    elif char == 'J' or char == 'K' or char == 'L':
        real_num += '5'
    elif char == 'M' or char == 'N' or char == 'O':
        real_num += '6'
    elif char == 'P' or char == 'Q' or char == 'R' or char == 'S':
        real_num += '7'
    elif char == 'T' or char == 'U' or char == 'V':
        real_num += '8'
    elif char == 'W' or char == 'X' or char == 'Y' or char == 'Z':
        real_num += '9'
    else:
        real_num += char

  print(real_num)

main()

Output:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
Please Enter the number in the format of XXX-XXX-XXXX
:555-GET-FOOD
555
Rarblack
  • 4,559
  • 4
  • 22
  • 33