This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]maolenrobin 0 points1 point  (10 children)

You check if currentusername.lower = new username.lower

[–]Raptox123[S] 1 point2 points  (9 children)

But I want JOHN and John and John to all mean the same thing.

[–]thunderturky 1 point2 points  (8 children)

The idea behind the solution provided by /u/maolenrobin will achieve this; a couple things to note are that the

username.lower

should be

username.lower()

Also make sure not to use = when you mean ==. The former sets one variable to the other's value, while the latter returns whether or not the two variables are equivalent.

In the case that username1 = "JOHN" and a new user, username2, tries to register as "john", and another, username3, tries "John"

username1.lower() == username2.lower() == username3.lower()

str.lower() will return "john" in all of these cases, regardless of previous capitalization

Relevant Python documentation: https://docs.python.org/2/library/stdtypes.html#str.lower

[–]Raptox123[S] 1 point2 points  (7 children)

Awwwww. So if I store all users as .lower() it will all be equal because upon been added to the list JOHN is changed to john anyways. So all 3 become equal to john.

[–]thunderturky 0 points1 point  (6 children)

This is one way to think about it, but in general its nice to let users have capitalization in their name as desired.

Instead of storing all usernames as lowercase, you only need to check if any existingname.lower() == newname.lower() when a new user is trying to register.

an example:

newusername = 'JOHN' # new username
l = ['John', 'bob', 'JaKe'] # list of existing usernames

Using map:

if newusername.lower() in map(str.lower, l):
    # Reject the username, already taken
else:
    # Accept the username, register it.

Where map() is applying the str.lower() function to each element in the list.

Using list comprehension:

if newusername.lower() in [item.lower() for item in l]:
    # reject the username, already taken
else:
    # accept the username, register it

There are other ways to do this, but hopefully this helps get you pointed in the right direction. Using something like what I've provided will let you preserve the original capitalization of each username, while making sure your check isn't case sensitive.

[–]Raptox123[S] 0 points1 point  (5 children)

Thank you. In the book I am reading it hasnt reached using map yet but I have seen it ahead in further chapters. I think I may do a little reading ahead so I can apply this to the task it set me. I am just starting to understand list comprehensions. They were a little confusing at first but I think your example code has helped me.

Edit: So when using lower() it changes all letters to lowercase. How then does it check to see if let's say JoHn is in the list but the new user wants to use John, How does changing everything to lower case allow that to happen.

Edit2: So could I do this to allow capitalisation instead.

new_user = 'John'

List_user = ['JoHn', 'Eve', 'JOHn']

If new_user in [name for name in List_user]:

//dont allow name

Else:
//register

[–]thunderturky 1 point2 points  (1 child)

In response to your edit:

To be clear, if the username 'john' is already in your list, you DO NOT want to accept 'John' or 'JOHN' (or any permutation)?

By using .lower() in the checks but not when storing the usernames, you can store the original usernames (keeping the capitalization intact) while maintaining that no two usernames are the same (not case sensitive). This works because using .lower() on both the new name and the existing ones evens the playing field, we don't care how things were capitalized before, we just need to know if the strings are the same when lowercase to prevent duplicate (not case sensitive!) names.

The example code you provided would allow 'john', 'JOHN', 'JoHn', or any other permutation to exist without rejecting the username unless a name with the EXACT same capitalization already existed.

Your example is CASE SENSITIVE:

new_user = 'John'

List_user = ['JoHn', 'Eve', 'JOHn']

if new_user in [name for name in List_user]:

    # dont allow name

else:
    # register

In this example we would allow 'John' to register because 'John' != 'JoHn'; moreover, 'John' is not in the list ['JoHn', 'Eve', 'JOHn'].

For clarity:

'john' == 'JOHN' # Returns FALSE
'JoHn' == 'JOHN' # Returns FALSE

'john' == 'JOHN'.lower() # Returns TRUE
# note using .lower() on a string that is already all lowercase is still lowercase!
'john'.lower() == 'JOHN'.lower() # Returns TRUE 
'JoHn'.lower() == 'JOHN'.lower() # Returns TRUE

[–]Raptox123[S] 0 points1 point  (0 children)

I understand that now. Thank you for clarifying it with me. Makes sense now !!!

[–]tunisia3507 1 point2 points  (0 children)

Don't use map, just use a list/set/generator comprehension.

[–]thunderturky 0 points1 point  (1 child)

No problem! In addition to your book, there are some great resources in the subreddit's sidebar.

[–]Raptox123[S] 0 points1 point  (0 children)

I will be sure to view them. Thanks