在本练习中我从字符串中删除元音有什么问题?

时间:2016-11-07 22:31:04

标签: python

练习它返回给定的字符串而不需要任何元音,例如 "车"会回来" cr"。

def anti_vowel(text):
    #returns text without vowels
    answer = ""
    i = 0
    while i <= len(text) -1:
        for text[i] in "aeiouAEIOU" = False:
            answer += text[i]
            i += 1   
    answer = answer.join(text)

    return answer

1 个答案:

答案 0 :(得分:3)

问题出在这一行:

for text[i] in "aeiouAEIOU" = False:

=用于分配,用以下内容替换整行:

for text[i] not in "aeiouAEIOU":

你也有缩进问题,你的整个功能应该是这样的:

def anti_vowel(text):
    #returns text without vowels
    answer = ""
    i = 0
    while i <= len(text) -1:
        for text[i] not in "aeiouAEIOU":
            answer += text[i]
        i += 1   
    answer = answer.join(text)

    return answer

这将是Pythonic版本:

def anti_vowel(text):
    return ''.join(c for c in text if c.lower() not in "aeiou")