Been sitting on this minor problem a few days now, I don't know if I have it all wrong or just missed out on something.
The Objective: From each word in a sentence - Find the first vowel, remove the letters after that vowel from the word and multiply the remaining letters by 3.
The Example: If I have the sentence: "Hello World" the wanted output should be "HeHeHe WoWoWo".
My Code:
def bebis(inrad):
utrad = ""
inrad = inrad.split()
for tkn in inrad:
for tkn1 in tkn: #Eftersom tkn ar ordlista nu.
if tkn1 in vokaler:
count = len(tkn1)
utrad += tkn1
elif tkn1 in konsonanter:
utrad += tkn1
return utrad[:count+1]*3
print("Bebisspraket:",bebis(inrad))
My Thoughts: I split the sentence into a lists of words using split(). Then I use two for loops, one that should go through each word and the other one that should go through each letter in every word. If it finds a vowel, count where it is and then return the letters to the first vowel of the word.
My Problem: The output only gives me the first WORD in a sentence and breaks from there. So "Hello World" yields "HeHeHe" leaving me super frustrated. Why does it not go through the rest of the sentence?
解决方案
How about something like this:
import re
def bebis_word(word):
first_vowel = re.search("[aeiou]", word, re.IGNORECASE)
if first_vowel:
return word[0:first_vowel.start() + 1] * 3
else:
return ''
def bebis(sentence):
words = [bebis_word(word) for word in sentence.split()]
return " ".join(words)
print bebis("Hello World")
Output:
HeHeHe WoWoWo