Waylander
Waylander

Reputation: 49

Having trouble writing a letter replacement script in Python

I've been trying to write a scrip that would turn the input to lowercase, erase all vowels and put a period before every consonant. But my script always gets stuck/timing out. Can you help me out?

input = raw_input()

input1 = input.lower()

input_list = list(input1)


for letter in input_list:
    if str(letter) == "a" or str(letter) == "o" or str(letter) == "y" or str(letter) == "e" or str(letter) == "u" or str(letter) == "i":
        input_list.pop(input_list.index(letter))
    else:
        input_list.insert(input_list.index(letter) - 1, ".")


print ''.join(input_list)

Upvotes: 0

Views: 48

Answers (3)

Zion
Zion

Reputation: 1610

word = "HeLLO"
print("".join(["." + i for i in re.sub("[aeiou]","",word.lower())]))

output:

".h.l.l"
  1. no vowels
  2. all lowercase
  3. periods appear only before consonants not after

Upvotes: 0

Kasravnd
Kasravnd

Reputation: 107297

First of all you shouldn't change an iterable in iterating time, then instead of using multiple or you can simply put the vowels in a set and check the membership with in operand also for removing a certain item you can use a list comprehension :

input = raw_input()
>>> input = raw_input()
this is a test

>>> ''.join(['.{}'.format(i) for i in input if i not in 'aoyeui'])
'.t.h.s. .s. . .t.s.t'

Upvotes: 4

Brendan W
Brendan W

Reputation: 3453

You insert the period, and then find yourself back at the same consonant, so then you insert another period, and on and on and on...

So you're stuck in an infinite loop.

Upvotes: 1

Related Questions