lundi 31 août 2020

What is a good way of making usernames case insensitive in Python? (Beginner Level)

I'm still a programming student so this might be very easy for you to answer.

I'm writing some code from the book 'Python Crash Course'. The task I'm on has me 'Make sure my comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted. To do this you need to make a copy of current_users containing the lowercase versions of all existing users.'

Which in my code I have. Except it doesn't work.

current_users = ['ted', 'jed', 'red', 'ned', 'dr fred']

new_users = ['ted', 'jed', 'bernard', 'hoagie', 'laverne']

for username in new_users:
    if username in current_users:
        print(f"Username {username} is unavailable. Please choose a different username.")
    else:
        print(f"Username {username} is available for use.")

Output: Username ted is unavailable. Please choose a different username. Username jed is unavailable. Please choose a different username. Username bernard is available for use. Username hoagie is available for use. Username laverne is available for use. [Finished in 0.3s]

If I change 'ted' to 'Ted' or 'TED' it outputs:

Username Ted is available for use. Username TED is available for use.

It shouldn't. Have I missed something? Or did Python lose the function for case insensitivity in a recent update?

EDIT - Thanks to those answered this constructively. It says I shouldn't comment to say thanks so I'll do it here.

Aucun commentaire:

Enregistrer un commentaire