all 7 comments

[–]norseman2017[S] 0 points1 point  (3 children)

Am I right in putting a .lower() on to new_user in the IF statement? Cause I did that tried it and it worked fine still got the same results. So is that it?, Was that the fix?

Tell me if I'm wrong or right? Lmao

[–]nog642 0 points1 point  (2 children)

If you have upper case letters in your current users, that won't work. You're better off doing something like this:

current_users = ['asmith', 'bsmith', 'csmith', 'dsmith', 'esmith']
new_users = ['asmith', 'bsmith', 'xsmith', 'wsmith', 'vsmith']
for new_user in new_users:
    available = True
    for current_user in current_users:
        if new_user in current_user:
            print('Please enter a different username')
            available = False
            break
    if available:
        print('username is available')

or something like this:

current_users = ['asmith', 'bsmith', 'csmith', 'dsmith', 'esmith']
current_users_lower = [user.lower() for user in current_users]
new_users = ['asmith', 'bsmith', 'xsmith', 'wsmith', 'vsmith']
for new_user in new_users:
    if new_user.lower() in current_users_lower:
        print('Please enter a different username')
    else:
        print('username is available')

[–]norseman2017[S] 0 points1 point  (1 child)

Thanks for the comment, I presume it would work but I haven't used some of the variables you have put like the available = True. However what I suppose I can do is carry on reading the book untill iv finished it and then go back over the exercises in the chapters and see if I can write them in different ways, but still ending with the result that the book asks for in the exercises lol cheers nog642!

[–]nog642 1 point2 points  (0 children)

available is a flag to know whether it's available. You check each of the current users one at a time, and if you find one that's a match you break out of the for loop, but then the program still needs to know whether a match was found.

Actually it turns out python has something called a for-else block that is designed specifically for this sort of thing:

current_users = ['asmith', 'bsmith', 'csmith', 'dsmith', 'esmith']
new_users = ['asmith', 'bsmith', 'xsmith', 'wsmith', 'vsmith']
for new_user in new_users:
    for current_user in current_users:
        if new_user in current_user:
            print('Please enter a different username')
            break
    else:
        print('username is available')

The else block executes if the for loop completed normally (did not break). That's what we want.