-1

Dears,

I want to write a function in Python, given a string, returns a new string in which the vowel will remain as it is and the consonants will be replaced with grec alphabet : α, β, γ, δ, ε, ...., Ω ω. And once there is a duplicate consanant, same letter will be used :

Let me clarify this by examples :

String "Better" :

1 - "B" 1st Consonant will be replaced by 1st letter in alphabet "α"
2 - "e" is **vowel** will be kept as it is "e"
3 - "t" 2nd Consonant will be replaced by 2nd letter in alphabet "β"
4 - "t" 2nd Consonant ( duplicate) will be replaced by 2nd letter in alphabet "β"
5 - "e" is vowel will be kept as it is "e"
6 - "r" 3rd Consonant will be replaced by 3rd letter in alphabet "γ"

So we'll end up with the following string : "αeββeγ"

Here's the code after Rouven's suggestions :

import PySimpleGUI as sg

a = "αβγ" # replace with the complete greek alphabet
s = "Better"
vovels = "AEIOUaeiou"
already_replaced = {}
new_string = ""
counter = 0

layout = [

[sg.Text('Word:', size=(15,1)), sg.Input(enable_events=True, key='WORD', 
 size=(25,1)),
 sg.Text('Result:', size=(15,1)), sg.Input(enable_events=True, 
 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':

    for c in s:
        if c in vovels:
            new_string += c
        elif c in already_replaced.keys():
            new_string += already_replaced[c]
        else:
            new_string += a[counter]
            already_replaced[c] = a[counter]
            counter += 1
    print(new_string)
  window.close()

Thanks in advance

1 Answers1

1

This should work:

a = "αβγ" # replace with the complete greek alphabet
s = "Better"
vovels = "AEIOUaeiou"
already_replaced = {}
new_string = ""
counter = 0
for c in s:
     if c in vovels:
         new_string += c
     elif c in already_replaced.keys():
         new_string += already_replaced[c]
     else:
         new_string += a[counter]
         already_replaced[c] = a[counter]
         counter += 1
 
hedg3hog
  • 11
  • 4
  • Thanks @Rouven for prompt feedback, Once applying your recommendations it's printing the word "αeββeγ" each time I write a letter. Could you please advice – PyiPath2022 Nov 05 '22 at 17:23
  • You have to replace the value of the variable s with your text, and mabye ad an elif statement where you handle if the character is a space (hope I‘m understanding your question in the right way) – hedg3hog Nov 05 '22 at 22:02
  • Thanks @Rouven for update, for submitting "s" , it's OK but the result will not returning as expected. I want to input the string in an IputText field and get the result in 2nd InputText. In case you execute my above script you can notice the issue. – PyiPath2022 Nov 05 '22 at 22:25
  • replace s (for c in s) with values[1] (like in this [example](https://www.geeksforgeeks.org/user-input-in-pysimplegui/) , then write a function that sends the new_string to your output field – hedg3hog Nov 06 '22 at 13:37
  • Hello @Rouven, I change as per your request : for c in values[1]: , unfortunately i got the following error: for c in values[1]: ~~~~~~^^^ KeyError: 1 – PyiPath2022 Nov 08 '22 at 08:43
  • your key is "WORD" because you set it so: > key='WORD', – hedg3hog Nov 08 '22 at 14:32
  • Didn't get your point!!! – PyiPath2022 Nov 08 '22 at 15:03