Looping Through 2 lists?!

Ok, I’m working from the python crash course and I’ve designed two lists to loop through usernames. I’ve got it to work like it should Except if a username has numbers with it.

current_users = ['wickdwayz', 'olive', 'black panther',
                      'monkey12', 'elevenbravo', 'admin', '']

new_users = ['wickdwayz', 'elevenbravo', 'sanitizer12', 'water_head69', '420_lover', ]

user = '420_lover'

if user.lower() not in current_users:
    if user.lower()  not in new_users:
        print('You may use that username!')
else:
    print('Sorry that username is already in use. Please select another username.')

If I run this with the current “user”, I get no syntax error or anything else. It returns blank.
Can someone help me with this please?

The else belongs to the first if condition, which is currently true, but the second if condition is false, but has no else clause, thus nothing happens.

Thank you for your reply @stetim94 . Do you know how I could fix it? I am still not grasping why the usernames with numbers return nothing?:tired_face:

it has nothing to do with the usernames with numbers

lets walk through the throw of your program, starting here:

if user.lower() not in current_users:

when this condition is true, it will execute the code nested in the if body:

    if user.lower()  not in new_users:
        print('You may use that username!')

however, when this if condition is false, there is no else clause to execute, so you get no ouput

when user not in current users and not in new_users you get output (you may user that username)

when user in current user, you get output that the username is already taken

but when user not in current user but in new users, you don’t get any output, why do you expect output in this scenario? And if so, which output do you expect?

of course i know how to fix it, but i am helping you understand how you can fix it, you can learn more from that :slight_smile:

why don’t you use a logical operator to make life easier on yourself:

https://www.programiz.com/python-programming/operators

1 Like

@stetim94, Hey thank you very much. I was oblivious to what I had going on. When I ran a username from my current users that wasn’t in my new users, I realized what you were saying. Also, I’m using VS Code and realized the lines of my first if is accompanied by the else. And my 2nd… well I get the idea now. So, This is what I came up with now.

current_users = ['wickdwayz', 'olive', 'black panther',
                      'monkey12', 'elevenbravo', 'admin', '']

new_users = ['wickdwayz', 'elevenbravo', 'sanitizer12', 'water_head69', '420_lover', ]

user = 'SANitizer12'

if user.lower() not in current_users and user.lower() not in new_users:
    print('You may use that username!')
else:
    print('Sorry that username is already in use. Please select another username.')

And thank you for not just giving me the answer. I have seen with most help topics that an answer is not given, but learning the how’s and why’s of the code is the preferred method of assistance.

Now you get to the right answer yourself (with a little guidance), but this way you learn a more then when you are just given the answer. Nice job figuring it out :slight_smile:

i can also recommend this:

https://code.visualstudio.com/docs/python/debugging

then you can “walk” through your code, takes a bit too learn, but in the long run will pay you back a thousand fold (okay, maybe not a thousand, but a lot)

1 Like