1
votes

I am trying to create a program in Python that encodes the numbers with strings. For example, the program should take a command line argument like this "Call 753-293-1 Today" and produce an output like this "Call seven-five-three-two-nine-three-one Today"

So far I am unable to produce the expected result. My code so far encodes the numbers to strings but I do not know how to put the "-" to separate the string of number. Please can anyone help me out here. Thanks

import argparse


# --------------------------------------------------
def get_args():
    """Get command-line arguments"""

    parser = argparse.ArgumentParser(
        description='Jump The Five',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('text',
                        metavar='str',
                        help='Input text')

    parser.add_argument('-s','--strings',action='store_true',
                        help= 'Whether to encode numbers to string')

    return parser.parse_args()


# --------------------------------------------------
def main():
    """Make a jazz noise here"""

    args = get_args()
    text = args.text
    strings = args.strings

    jumper = {"1":"9","2":"8","3":"7","4":"6","5":"0","6":"4",
              "7":"3","8":"2","9":"1","0":"5",}


    number_to_string = {"1":"one","2":"two","3":"three","4":"four",
                        "5":"five","6":"six","7":"seven","8":"eight",
                        "9":"nine","0":"zero"}

    new_text = ""
    keys = ''
    num = ""

    for i in text:
        new_text +=  number_to_string.get(i,i)
    print(new_text)



# --------------------------------------------------
if __name__ == '__main__':
    main()
Do you understand what new_text += number_to_string.get(i, i) does? Specifically, what is the += doing? Can you think about how you'd modify this line to add a hyphen before adding the word? - Pranav Hosangadi
However, a better approach would be to create a list containing only the numbers translated to words, and then join that list on hyphens using str.join docs.python.org/3/library/stdtypes.html#str.join - Pranav Hosangadi