Home > other >  Word Conversion to Numbers in Dynamic way
Word Conversion to Numbers in Dynamic way

Time:11-01

Dears,

I'm converting words to numbers than removing vowels, it's working fine except when word are starting with vowel the program stops working ...sounds logic since the tool is removing vowel each time it encouter one of the letters from this list: {'a','e','i','o','u'} , can we get ride of this issue by any trick or idea ?

Example : WORD {'WORD': 'friend', 'NUMBER': '6-18-9-5-14', 'RESULT': 'FRN'}

For Failed cases, I got the following :

WORD {'WORD': 'a', 'NUMBER': '', 'RESULT': ''}
Traceback (most recent call last):
  File "<FILE_PATH>\Example.py", line 37, in <module>
    result3 = result2[1:] if result2[0].lower() in filter_out else result2
                             ~~~~~~~^^^
**IndexError: string index out of range**

    import PySimpleGUI as sg

    alphabet = {'a':'1','b':'2','c':'3','d':'4','e':'5','f':'6','g':'7','h':'8','i':'9','j':'10',
    'k':'11','l':'12','m':'13','n':'14','o':'15','p':'16','q':'17','r':'18','s':'19','t':'20',
    'u':'21','v':'22','w':'23','x':'24','y':'25','z':'26'}

    vowels = {'a','e','i','o','u'}

    layout = [

    [sg.Text('Word:', size=(15,1)), sg.Input(enable_events=True, key='WORD', size=(25,1))],
    [sg.Text('Number:', size=(15,1)), sg.Input(key='NUMBER', size=(25,1))],
    [sg.Text('Result:', size=(15,1)), sg.Input(key='RESULT', size=(25,1))],
    ]

    window = sg.Window('MAIN',  layout, finalize=True,element_justification='c')


    while True:
    event, values = window.read()
    print(event, values)

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

    elif event == 'WORD':
        string1 = values['WORD']
        string2 = '-'.join([alphabet[char] if char in alphabet else char for char in string1])
        window['NUMBER'].update(string2)

        result1 = ''.join([letter for letter in string1 if letter.lower() not in vowels])
        result2 = ""
        for i in result1:
            if i.lower() not in result2 and i.upper() not in result2:
                result2  = i
        filter_out = {'d','t'}
        result3 = result2[1:] if result2[0].lower() in filter_out else result2
        result3 = result3[:-1] if result3[-1].lower() in filter_out else result3
        window['RESULT'].update(value=result3.upper())

        window.close()

Thanks in advance

CodePudding user response:

The filter_out is a dictionary but you are trying to use it as a list. Make it ['d','t'] instead of {'d','t'}

import PySimpleGUI as sg

alphabet = {'a':'1','b':'2','c':'3','d':'4','e':'5','f':'6','g':'7','h':'8','i':'9','j':'10',
'k':'11','l':'12','m':'13','n':'14','o':'15','p':'16','q':'17','r':'18','s':'19','t':'20',
'u':'21','v':'22','w':'23','x':'24','y':'25','z':'26'}

vowels = {'a','e','i','o','u'}

layout = [

[sg.Text('Word:', size=(15,1)), sg.Input(enable_events=True, key='WORD', size=(25,1))],
[sg.Text('Number:', size=(15,1)), sg.Input(key='NUMBER', size=(25,1))],
[sg.Text('Result:', size=(15,1)), sg.Input(key='RESULT', size=(25,1))],
]

window = sg.Window('MAIN',  layout, finalize=True,element_justification='c')


while True:

    event, values = window.read()
    print(event, values)

    if event == sg.WIN_CLOSED or event == 'Exit':
        break

    elif event == 'WORD':        
        
        string1 = values['WORD']
        string2 = '-'.join([alphabet[char] if char in alphabet else char for char in string1])
        window['NUMBER'].update(string2)
        
        result1 = ''.join([letter for letter in string1 if letter.lower() not in vowels])
        result2 = ""

        for i in result1:
            if i.lower() not in result2 and i.upper() not in result2:
                result2  = i

        for i in ['d', 't']:

            result3 = result2.replace(i.lower(), "").replace(i.upper(), "")

            result2 = result3
            
        window['RESULT'].update(result3.upper())

    #window.close()

When you type a the result2 is empty, so the it would be result = ''. Therefore it gave an Index Error when you called it using result2[1:].

  • Related