This is supposed to be part of a function that replaces consonants with '$' until the "ch" character is reached, where it will then stop.
Examples:
vowels_or_not(‘abcdeAghi’, ‘A’) should return ‘a$$$e’
vowels_or_not(‘aaaaA’, ‘A’) should return ‘aaaa’
My issue is that it isn’t doing anything if the input string is only one character long. vowels_or_not(a, X) should return a and vowels_or_not(x, A) should return $. Everything else works fine. I tried fixing it but I still don’t see anything wrong with the code, but then again I’m a beginner! Any help would be greatly appreciated.
def vowels_or_not (st, ch)
vowels = ('a','e','i','o','u','A','E','I','O','U')
flag = True
a = 0
aux = ''
while flag is True:
for i in range(len(st)):
if (st[i] == ch):
flag = False
break
else:
if (st[i] in vowels):
aux = aux + st[i]
a = a + 1
if (st[i] not in vowels):
aux = aux + '$'
a = a + 1
return aux
Your problem is your while loop never ends if the last character in
stisn’t the same asch.But you don’t really need that while loop as it is redundant, you only need the for-loop: