I have a lot of repetition in my code, a prime example is when I'm doing a simple check to see if the first letter of a string is a vowel or not. The code I have is as follows :
if word[0] == 'a' or word[0] == 'e' or word[0] == 'i' or word[0] == 'o' or word[0] == 'u':
print 'An', word
else:
print 'A', word
This works fine but the amount of repetition leads me to think there could be an easy way to shorten this, I just don't know of it. I also tried this code:
if word[0] == 'a' or 'e' or 'i' or 'o' or 'u':
print 'An', word
else:
print 'A', word
However, this code returned True
for every word, regardless of beginning letter.
So, just to clarify. The code works fine and it fully functional and I know I could define it as a function and just use that but it seems like it could easily be shortened and this knowledge would be useful on multiple projects.