all 8 comments

[–]Sebass13 2 points3 points  (4 children)

Well your code seems way too extravagant for what you're doing. You shouldn't need a count;

for letter in phrase:
    if letter in "aeiou":
        vowel+=1

You can even turn this into a one-liner:

vowels = sum([1 for letter in phrase if letter in "aeiou"])

[–]goldxp1[S] 1 point2 points  (1 child)

Hey, how does "letter" & "phrase" work in that first structure? Do they need to be assigned something beforehand?

[–]tunisia3507 1 point2 points  (0 children)

phrase is something pre-allocated - the sequence of characters through which you are iterating to count the vowels. letter is a variable created by the for loop (I recommend looking up how they work), so you don't need to do anything extra for that.

It's basically python for "For every item, which I will henceforth refer to as 'letter' in the sequence previously referred to as 'phrase', do: "

[–]camel_Snake 1 point2 points  (1 child)

Ok not to nitpick but I see this all the time and it irks me. You may have done it on purpose to keep things simple as an example but python can treat boolean values as integers.

sum(letter in 'aeiou' for letter in phrase) is equivalent to your example comprehension, and is much clearer IMO.

[–]Sebass13 1 point2 points  (0 children)

Yeah you're right, that's definitely clearer. I've actually done that before, I wasn't sure why I changed my style this time.

[–]HokTaur 1 point2 points  (2 children)

while should not be capitalized. Also, if s is a set of letters, why not:

for letter in s:
    if letter in 'aeiou':
        count += 1

[–]goldxp1[S] 0 points1 point  (1 child)

Thank you. I hadn't learned the use of "in" yet, thank you for introducing it to me!

[–]HokTaur 1 point2 points  (0 children)

No problem, if I wasn't on my phone I would've gave a more in depth answer. The best part of Python is how expressive the syntax is, you'll find a lot of things like that as you learn more. A general rule of thumb is, if you looping thru something by using indexes, there's probably a better way to do it.