When the user name is 'HIQUINHO'
, when calling lower
he becomes hiquinho
, and you compare it to the values on the other list, which only has 'Hiquinho'
, why he thinks this user has not yet been used.
Therefore, you should compare each new user with the lowercase version of the current users:
current_users = [ 'Hiquinho', 'pup', 'billy', 'jp', 'pichuto', 'schuh']
new_users = [ 'HIQUINHO', 'sara', 'Billy', 'amalia', 'lucca', 'Schuh']
current_users_lowercase = list(map(str.lower, current_users))
for new_user in new_users:
if new_user.lower() in current_users_lowercase:
print(f'Nome {new_user} já em uso, forneça outro ID')
else:
print(f'Seja bem vindo, somos muito gratos pelo seu login {new_user.title()}!')
Also note that you don’t need to test the list new_users
is empty. If it is, it doesn’t even enter the for
, then the if new_users:
is redundant.
And the condition of elif
is also redundant. If the user is in the list, he if
, and if it’s not, it goes to the else
(I mean, you don’t need to test this again with elif
, can use else
straightforward).
The exit code above is:
Nome HIQUINHO já em uso, forneça outro ID
Seja bem vindo, somos muito gratos pelo seu login Sara!
Nome Billy já em uso, forneça outro ID
Seja bem vindo, somos muito gratos pelo seu login Amalia!
Seja bem vindo, somos muito gratos pelo seu login Lucca!
Nome Schuh já em uso, forneça outro ID
It is worth remembering that, depending on the characters that may have in the user’s name, not always lower()
works. For example, the German character ß
is equivalent to "ss", but lower()
does not change it. For these - and many others - cases, the documentation recommends the use of casefold
:
print('ß'.lower()) # ß
print('ß'.casefold()) # ss
I mean, you could just switch to:
current_users = [ 'Hiquinho', 'pup', 'billy', 'jp', 'pichuto', 'schuhss']
new_users = [ 'HIQUINHO', 'sara', 'Billy', 'amalia', 'lucca', 'Schuhß']
# usar casefold em vez de lower
current_users_lowercase = list(map(str.casefold, current_users))
for new_user in new_users:
if new_user.casefold() in current_users_lowercase: # usar casefold em vez de lower
print(f'Nome {new_user} já em uso, forneça outro ID')
else:
print(f'Seja bem vindo, somos muito gratos pelo seu login {new_user.title()}!')
Of course, if you only have ASCII characters, use lower
is enough.
thank you very much!
– Henrique FH