Question

I am trying to make all vowels in the second half of a string uppercase. This is what I did so far, I can't seem to get the result I want.

#Ask user for word
word = str(input("Please enter a word: "))

#Count word length
wordLen = len(word)

#Count half of word length
halfWordLen = int (wordLen/2)

#Obtain 1st and 2nd half of the word
firstHalf = word[:halfWordLen]
secondHalf = word[halfWordLen:]

#Determine vowels
vowels = set(['a','e','i','o','u'])

#Iterate through 2nd half to find vowel.
#Then, uppercase the vowels, and display new word.
for char in secondHalf:
    if char in vowels:
        newWord = firstHalf + secondHalf.replace(char,(char.upper()))
        print ("The new word is: ",newWord)

Results:

Please enter a word: abrasion
The new word is:  abrasIon
The new word is:  abrasiOn

It's supposed to be:

Please enter a word: abrasion
The new word is:  abrasIOn 
Was it helpful?

Solution

There are two issues with your code. First, when you are replacing the vowel in your second half, you are only doing it temporarily. You need to perform that operation in a separate line and save it in the second half variable.

Also, you are printing the your temporary result each time you go through the loop. If you only want it to only print once, just decrease the level of indent so that it is outside the loop. Here is how I would restructure it.

for char in secondHalf:
    if char in vowels:
        secondHalf = secondHalf.replace(char,(char.upper()))
newWord = firstHalf + secondHalf
print ("The new word is: ",newWord)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top