Python Forum
Begginer coding problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Begginer coding problem (/thread-7823.html)



Begginer coding problem - wiktor - Jan-26-2018

Hello,

Im pretty new to python and programming and I need some help. I need to check if user name is already in usage by another one. That was good and I dealt with it pretty fast. But then I found that if new user name is "Mark" and current user name is "MARK" It says that it's all good and this user doesn't have to change the name, but that's wrong. How can I make it to say that user name needs to be changed? I just need to make sure that letter size doesn't matter in comparing these two. I'm really thankful for all help!

That's my code so far:

current_users = ['alex', 'zuzia', 'zoie', 'maciej', 'wiktor', 'MARK']
new_users = ['alex', 'monika', 'pawel', 'henryk', 'adam', 'Mark']

for new_user in new_users:
	if new_user in current_users:
		print("You need to change users name!")
	else:
		print("You dont have to choose diffrent users name!")



RE: Begginer coding problem - j.crater - Jan-26-2018

Hello and welcome to Python and our forums!
Usually this problem is tackled by converting all the strings to lowercase. Python's builtin lower() function is used for that. For example:

current_users = ['alex', 'zuzia', 'zoie', 'maciej', 'wiktor', 'MARK']
new_users = ['alex', 'monika', 'pawel', 'henryk', 'adam', 'Mark']

current_users_lower = [x.lower() for x in current_users]
new_users_lower = [x.lower() for x in new_users]

for new_user in new_users_lower:
    if new_user in current_users_lower:
        print("You need to change users name!")
    else:
        print("You dont have to choose diffrent users name!")
Lines 4 and 5 are list comprehensions, they create a new list with the "formula" provided. I think you can easily decipher how it works.
It is possible to do it without list comprehensions too, but it would be a less elegant solution with loops.


RE: Begginer coding problem - wiktor - Jan-26-2018

Thank you so much!

Well, actually i copied it just perfectly, but I don't understand that. I will appreciate you if you would make easier and less elegant form because i'm on too low lever for now.