mardi 25 septembre 2018

.lower/.upper function in combination with for loop and if/elif statements [duplicate]

This question already has an answer here:

I have just started learning how to code with Python 3. i want to write a code that identifies which strings in a list have a certain length and for example if they have a certain length for them to be lower cased and if they don't have that certain length in characters, for them to be uppercased. So this is the code I was trying to make it work:

Phrase = ['apple','orange','whatnot']

if len(e) == 6:
    e.upper() for e in Phrase
else:
    e.lower() for e in Phrase

but I obviously get a syntax error. I know that if write the following code:

[e.lower() for e in Phrase if len(e) > 6] 

It works but I don't know how to add else and elif statements to that.

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire