#πŸ”’ Help with checking for vowels at end of object in list

48 messages Β· Page 1 of 1 (latest)

remote schooner
#
#initializing friends list
friends = ['Alice', 'Bob', 'Charlie', 'Debbie']

#converting objects in list to all upppercase
uppercase_friends = [friend.upper() for friend in friends]

#checking for whether last letter in each object in list ends in a vowel, if so, print statement
for friend in uppercase_friends:
    if friend[-1] == "e":
        print ("\'" +friend + " IS AWESOME!\'")

This prints out nothing, i tried different ways to word this and it either prints everything for times and doesnt check for a vowel or its nothing. Can i get help with posing this in a way that checks for last character of object in list for a vowel?

shadow lakeBOT
#

@remote schooner

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

clever bay
#

you'd need to compare to 'E'

#

strings are case sensitive

remote schooner
#

Yes that works, should I add the rest of the vowels separated by <or>

#

or do I need to write elif statements for each vowel?

clever bay
#

you can use the in operator to do this easily

#

if friend[-1] in 'AEIOU':

#

this checks if the letter exists as a substring inside AEIOU

#

which is a cheeky way of checking for all the vowels at once

remote schooner
#

conversely, i would put <not in> then correct

clever bay
#

for checking if it's isn't a vowel?

remote schooner
#

yes

#

i just tried it and it worked, ive been stuck on this for 3 hours!!

#

thank you so much

clever bay
#

you can if that was the only think you wanted to check, but since you're already checking if it is a vowel, you might as well use else

#

it's either a vowel, or it isn't

#

you don't need to check both conditions explicitly

remote schooner
#

I see, but I have to also convert those objects in the list to lower case so I thought it would not work

clever bay
#

why convert them to upper case if you then have to convert to lower case?

remote schooner
#

the question asks to conver to uppercase for thos ending in vowel and conver to lower case for those not

clever bay
#

I wouldn't convert the whole list to upper in the start then

#

just convert to what is required within each condition

#
friends = ['Alice', 'Bob', 'Charlie', 'Debbie']

for friend in friends:
    ...
lethal python
#

You can create a new list
And then append that list considering your conditions

clever bay
#

if you want to reliably make sure the last letter is the correct case, you can apply upper or lower during the condition

remote schooner
#

processing

clever bay
#
friends = ['Alice', 'Bob', 'Charlie', 'Debbie']

for friend in friends:
    if friend[-1].upper() == "E":
#

see how you can apply .upper() directly to friend[-1]?

remote schooner
#

i didnt know I could do that, I have to keep going back to notes and so I will try this to conver to during the conditions, but in this way I would have to write elif for each letter, correct?

clever bay
#

nope

#

you can still use in

remote schooner
#
or friend in friends:
    if friend[-1].upper() == "E":
    elif friend[-1].upper() == "A":
#so on...
#

oh i see i can use in "AEIOU"

clever bay
#
for friend in friends:
    if friend[-1].upper() in "AEIOU":
#

yup, exactly

remote schooner
#

omg I will try this out, thank you so much!

lethal python
#

You can also do

in 'aeiouAEIOU'

to cover both lower and upper case

clever bay
#

that's not necessary though if you're doing .upper()

remote schooner
#

I see way shorter if can put it all together, working on it rn

#
#initializing friends list
friends = ['Alice', 'Bob', 'Charlie', 'Debbie']

#converting objects in list to all upppercase
uppercase_friends = [friend.upper() for friend in friends]

#checking for whether last letter in each object in list ends in a vowel, if so, print statement
for friend in friends:
    if friend[-1].upper() in "AEIOUaeiou":
        print ("\'" +friend + " IS AWESOME!\'")
    else:
        print ("\'" +friend.lower() + " is ok...\'")
#

IT WORKEDπŸ₯Ή

#

why did python react with trash?

#

did i do something wrong?

clever bay
#

No, if you click that emoji, it will remove the embedded message

shadow lakeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.