Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework
#1
Please, how do i make my comparison case sensitive?
#program that simulates how websites ensure that everyone has a unique username . 
current_users = ['mary', 'michael', 'john', 'cyril', 'philippa']
new_users = ['mike', 'mary', 'joy', 'philippa', 'peace']

for new_user in new_users:
    if new_user.lower() in current_users.lower():
        print('Username not available')
        print('Please enter a new username')
    else:
        print('Username is available.')
Reply
#2
Get rid of the calls to the lower method on line 6, especially since current_users.lower() should cause an error. That's because current_users is a list, you can only use the lower method with strings.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
current_users = ['MARY', 'michael', 'john', 'cyril', 'philippa']
new_users = ['mike', 'mary', 'joy', 'philippa', 'peace']
current_users_lower= list(map(lambda x:x.lower(),current_users))
new_users_lower= list(map(lambda x:x.lower(),new_users))

for new_user in new_users_lower:
    if new_user in current_users_lower:
        print('Username not available ', new_user.lower())
        print('Please enter a new username')
    else:
        print('Username is available.')
As Ichabod mentioned, you can not called lower method on list object. I have corrected logic and display here. Please let me know if this is fine.

We have to convert each element of list to lower case before we compare them.
Reply
#4
Why 'case sensitive' is needed? 'Mary' and 'mary' are unique names.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Yes , that is why we need to convert to lower case when we are comparing. Python is case sensitive which mean 'A' == 'a' is false. So if user enter capital or lower case, still both should be same therefore we need to convert to lower case.
Reply
#6
(Jan-09-2020, 09:12 PM)prateekshaw Wrote: Yes , that is why we need to convert to lower case when we are comparing. Python is case sensitive which mean 'A' == 'a' is false. So if user enter capital or lower case, still both should be same therefore we need to convert to lower case.

As far I understand the objective of this assignment to check uniqueness (of usernames). In that sense 'mary' and 'Mary' are unique and should be allowed. If there are other requirements they should be stated (like: no other username with same characters but different case combinations allowed; means 'mary' and 'Mary', 'MARY' and 'mAry' are not unique usernames).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020