0

I am trying to add text with vowels in certain words (that are not consecutive vowels like ie or ei), for example:

Word: 'weird'

Text to add before vowel: 'ib'

Result: 'wibeird'

Thus the text 'ib' was added before the vowel 'e'. Notice how it didn't replace 'i' with 'ib' because when the vowel is consecutive I don't want it to add text.

However, when I do this:

Word: 'dog'

Text to add before vowel: 'ob'

Result: 'doboog'

Correct Result Should Be: 'dobog'

I've been trying to debug my program but I can't seem to figure out the logic in order to make sure it prints 'wibeird' and 'dobog' correctly.

Here is my code, substitute first_syl with 'ob' and word with 'dog' after you run it first with 'weird.

first_syl = 'ib'
word = 'weird'

vowels = "aeiouAEIOU"
diction = "bcdfghjklmnpqrstvwxyz"
empty_str = ""
word_str = ""
ch_str = ""
first_vowel_count = True

for ch in word:
    if ch in diction:
        word_str += ch
    if ch in vowels and first_vowel_count == True:
        empty_str += word_str + first_syl + ch
        word_str = ""
        first_vowel_count = False
    if ch in vowels and first_vowel_count == False:
        ch_str = ch
    if word[-1] not in vowels:
        final_str = empty_str + ch_str + word_str 

print (final_str)

I am using Python 3.2.3. Also I don't want to use any imported modules, trying to do this to understand the basics of strings and loops in python.

3
  • Note that you are not replacing the vowel but inserting a string in front of a vowel. If we would replace it, 'dog' would become 'dobg' after replacing 'o' by 'ob'. Commented Sep 23, 2012 at 21:35
  • In your first example, it doesn't look like you replaced the "e" at all- there's still an e in "wibeird" Commented Sep 23, 2012 at 21:36
  • Sorry I didn't mean to 'replace' I actually meant to add the text before the vowel, I apologize. I fixed that in the description now. Commented Sep 23, 2012 at 22:14

2 Answers 2

2

Have you considered regular expressions?

import re

print (re.sub(r'(?<![aeiou])[aeiou]', r'ib\g<0>', 'weird')) #wibeird
print (re.sub(r'(?<![aeiou])[aeiou]', r'ob\g<0>', 'dog')) #dobog
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I didn't mention this earlier, I'd rather not use imported modules and regex, trying to do this to understand the basics of strings and loops in python..thanks.
1

Never use regex when you don't have to. There's a famous quote that goes

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

This can easily be solved with basic if-then statements. Here's a commented version explaining the logic being used:

first_syl = 'ib' # the characters to be added
word = 'dOg'     # the input word

vowels = "aeiou" # instead of a long list of possibilities, we'll use the 
                 # <string>.lower() func. It returns the lowercase equivalent of a 
                 # string object.
first_vowel_count = True # This will tell us if the iterator is at the first vowel
final_str = ""           # The output.

for ch in word:
    if ch.lower() not in vowels:     # If we're at a consonant, 
        first_vowel_count = True     # the next vowel to appear must be the first in 
                                     # the series.

    elif first_vowel_count:          # So the previous "if" statement was false. We're 
                                     # at a vowel. This is also the first vowel in the 
                                     # series. This means that before appending the vowel 
                                     # to output, 

        final_str += first_syl       # we need to first append the vowel-
                                     # predecessor string, or 'ib' in this case.
        first_vowel_count = False    # Additionally, any vowels following this one cannot 
                                     # be the first in the series.

    final_str += ch                  # Finally, we'll append the input character to the 
                                     # output.
print(final_str)                     # "dibOg"

2 Comments

I like that quote, and wow, you must be a genius, I obviously overcomplicated things, learning this is difficult for me, but fascinating. Is there a way to message you on this site, I may have a few more questions because what I plan to do next is take every vowel after the first and add different sets of text before it (so second_syl, third_syl, etc.)?
@Ratman2050 There isn't a way to send private messages; this comment system is as private as it gets. If you have more questions, feel free to make more posts for them, I'm sure you'll get equally helpful feedback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.