python 如何以更好的方式编写此问题的解决方案

izj3ouym  于 2022-12-17  发布在  Python
关注(0)|答案(2)|浏览(359)

python如何解决这个问题:
如果单词包含两个连续的元音字母(a、e、i、o、u、y),则必须删除第二个字母,然后打印该单词。
我写了这段代码,我知道它并不完全正确,有些东西是没有意义的.
虽然它给了我所需的结果,但我发现它是不正确的,因为有太多的嵌套循环。
我想一个更好的和更容易的代码由python请。

def Convert(string):
    word = ["","","",""]
    word[:0] = string
    return word

word1 = str(input("Enter the word: ")).lower()
word = Convert(word1)
vowels = ["a","e","i","o","u","y"]
y = []

for i in range(1,len(word)-4):
  for j in range(5,len(vowels)):
    for x in range(6):
      for v in range(6):
       if word[i] == vowels[j-x] and word[i-1] == vowels[j-v]:                                                          
          word.remove(word[i])                                    
          print("".join(word))
          y.append("".join(word))
          break

if len(y)==0:
    print(word1)
r55awzrz

r55awzrz1#

VOWELS = "aeiouy"

def removeSecondVowel(word):
    for vowel in VOWELS:
        while 2 * vowel in word:
            word = word.replace(2 * vowel, vowel)

    return word

print(removeSecondVowel(input("Your word: ")))
wztqucjr

wztqucjr2#

这是我想到的。

def twoVowels(word):
    vowels = set('aeiouy')
    result = ""
    for i in range(len(word)):
        if word[i] in vowels and i < len(word)-1 and word[i+1] in vowels:
            continue
        else:
            result += word[i]
    return result
      
print(twoVowels(input("Enter word: "))) # Enter word: bookkeeper

结果:bokkeper

相关问题