Anti vowel : some confusion


#1



https://www.codecademy.com/zh/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096


Actually my code ran normally , but if I use different intended block for "return"
the result is different
I'm confused that ...if I code as second image , why the program didn't work for 3 times but only 1 time...


def anti_vowel(text):                  #**this is wrong program**
	new=[]
	v=['a','e','i','o','u']
	for i in text:
		if i.lower() not in v:
			new.append(i)
		return new         #at last I'll add join function here , but now I just want to figure out problem
print (anti_vowel('hello'))



def anti_vowel(text):                  #**this is right program**
	new=[]
	v=['a','e','i','o','u']
	for i in text:
		if i.lower() not in v:
			new.append(i)
	return new         #at last I'll add join function here , but now I just want to figure out problem
print (anti_vowel('hello'))


#2

If you've coded in js, you might want to relate that your curly brackets are "replaced" with indentation in py. [Doesn't matter really] The first "wrong" code shows that you are trying to return the conditional statement but then the second shows you want to return the output after is has looped for ....

Hence, why the second is much more appropriate to meet your need for anti_vowel.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.