0

I've made some code that has a list of new users and current users and checks to see if the username is available. If unavailable, it outputs a message saying so, otherwise outputs a message saying it's available. After this, I'm trying to append the elements of the new_users list to the current_users, on the condition that it doesn't already exist there. I would then like to print the current_users list to indicate the changes have occurred.

Here is my code so far - it only prints the current_users list without the changes still which is where my problem is.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

current_users_lower = [user.lower() for user in current_users]

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')

if new_user not in current_users_lower:
    current_users_lower.append(new_user)
print(current_users_lower)

Thank you for any help in advance. I know question formatting is really important here so i've tried my best to ask it as clearly as possible.

Thanks!

1
  • 1
    Thank you to the user who edited my code - I thought copying the code directly from my IDE it would carry the format into the post. Didn't realise it needed more work. Cheers! Commented Jul 9, 2020 at 2:23

5 Answers 5

1

IIUC, my advice would be to add the append as part of the for loop, while also using current_users_lower instead of current_users. All in all it would end up like this:

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

current_users_lower = [user.lower() for user in current_users]

for new_user in new_users:
    if new_user in current_users_lower:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')
        print("Appending new user to list")
        current_users_lower.append(new_user)

print(current_users_lower)
Sign up to request clarification or add additional context in comments.

3 Comments

you should also change new_user to lower
Thank you for the quick response and also assisting with my question formatting. Thanks a heap!!!
Cheers @slowlearner02 do come back if you have any other question, we're happy to help!
1

You were simply missing an indent.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

current_users_lower = [user.lower() for user in current_users]

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable")
    else:
        print(f'{new_user} available')
    if new_user not in current_users_lower:
        current_users_lower.append(new_user)
print(current_users_lower)

2 Comments

The if statement was not indented
Can't believe it was as simple as an indentation error. Thank you!
0

Your problem seems to be that you are only appending the last item of you new_users list to current_users_lower. Try adding the if statement that appends the list to your for loop.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

current_users_lower = [user.lower() for user in current_users]

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')

    if new_user not in current_users_lower:
        current_users_lower.append(new_user)

print(current_users_lower)

1 Comment

Thank you! Such a positive and quick response from everyone here. Not sure why I felt so intimidated to post this now haha
0

It's because the statement was not in the for statement.

 current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

current_users_lower = [user.lower() for user in current_users]

for new_user in new_users:
    if new_user in current_users:
        print(f"{new_user} unavailable\n")
    else:
        print(f'{new_user} available')
        current_users_lower.append(new_user)
print(current_users_lower)

Comments

0

use set operation. it makes code simple.

current_users = ['alice', 'bob', 'mary', 'OSCAR', 'Simon']
new_users = ['jack', 'daniel', 'sue', 'mary', 'chris']

current_users_lower = set([user.lower() for user in current_users])
new_users = set([user.lower() for user in new_users])

print(f'{new_users.intersection(current_users_lower)} are unavailable')
really_new = new_users.difference(current_users_lower)
print(f'{really_new} are available')

print(current_users_lower.union(new_users))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.