-1

I'm working trough the Python Crash Course book and I am doing this assignment.

Do the following to create a program that simulates how websites ensure that everyone has a unique username.

  • Make a list of five or more usernames called current_users.
  • Make another list of five usernames called new_users. Make sure one or two of the new usernames are also in the current_users list.
  • Loop through the new_users list to see if each new username has already been used. If it has, print a message that the person will need to enter a new username. If a username has not been used, print a message saying that the username is available.
  • Make sure your comparison is case insensitive. If 'John' has been used, 'JOHN' should not be accepted.

I can see if a username in one list is in another with.

current_users = ["John", "Admin", "Jack", "Ana", "Natalie"]
new_users = ["Pablo", "Donald", "Calvin", "Natalie", "Emma"]

for username in new_users:
    if username in current_users:
        print("Username unavailable.")
    else:
        print("Username available.")

Now for the case insensitive part I know about the .lower() method, but not sure how to convert a list item to lowercase in the loop. I could do if username.lower() in current_users, which wouldn't work because I need to covert current_users somehow. Is there a better way then just doing .lower() for both lists before the loop.

Community
  • 1
  • 1
dovla
  • 191
  • 2
  • 4
  • 11

3 Answers3

4

You should convert your current_users into a lowercase set and then do blazignly fast comparisons for each of your new users, just lowercased, e.g.:

current_users = ["John", "Admin", "Jack", "Ana", "Natalie"]
new_users = ["Pablo", "Donald", "Calvin", "Natalie", "Emma"]

current_users_lookup = {user.lower() for user in current_users}
for user in new_users:
    if user.lower() in current_users_lookup:
        print("Username {} unavailable.".format(user))
    else:
        print("Username {} available.".format(user))

Which would get you:

Username Pablo available.
Username Donald available.
Username Calvin available.
Username Natalie unavailable.
Username Emma available.
zwer
  • 24,943
  • 3
  • 48
  • 66
3

Convert everything to lowercase before doing your test:

current_users = ["John", "Admin", "Jack", "Ana", "Natalie"]
new_users = ["Pablo", "Donald", "Calvin", "Natalie", "Emma"]

current_users = [x.lower() for x in current_users] 
new_users = [x.lower() for x in new_users]

If you're new to Python this is called List Comprehensions.

for username in new_users:
    if username in current_users:
        print("Username unavailable.")
    else:
        print("Username available.")

Or if the first letter of the usernames is always capitalized, you can use .title()

for username in new_users:
    if username.title() in current_users:
        print("Username unavailable.")
    else:
        print("Username available.")
Rakesh Adhikesavan
  • 11,966
  • 18
  • 51
  • 76
1

You can create an new list with lowerCase characters and then to check with it.

current_users = ["John", "Admin", "Jack", "Ana", "Natalie"]
new_users = ["Pablo", "Donald", "Calvin", "Natalie", "Emma"]  

current_users_lower = map(str.lower,current_users)
for username in new_users:
  if username.lower() in current_users_lower:
    print("Username unavailable.")
  else:
    print("Username available.")
omri_saadon
  • 10,193
  • 7
  • 33
  • 58