我正在尝试将字符串后半部分的所有元音都设为大写。这是我到目前为止所做的,我似乎无法得到我想要的结果。
#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)
结果:
Please enter a word: abrasion
The new word is: abrasIon
The new word is: abrasiOn
它应该是:
Please enter a word: abrasion
The new word is: abrasIOn