Search code examples
pythonnumberscharacteralphabet

Python assignmen intvolving the conversion of the charaters within a string into numbers


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


Solution

  • 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()