你有
return
for-a循环中的语句,这就是为什么您的代码就是您的循环只执行一次的原因。把它放在循环之外,代码就可以正常工作了。
def anti_vowel(string):
for t in string:
if(t.lower()=='a' or t.lower()=='e' or t.lower()=='i' or t.lower()=='u'):
string.replace(t, " ")
print "test"
print string
return string
为了替换元音字符,您不能替换现有变量,因为Python中的字符串是不可变的。你可以试试这个
def anti_vowel(string):
for t in string:
if(t.lower()=='a' or t.lower()=='e' or t.lower()=='i' or t.lower()=='u'):
string=string.replace(t, " ")
print "test"
print string
return string