Python Forum
lowercasing to strings before comparison
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lowercasing to strings before comparison
#1
I have some code for logging in. Like most logins, I don't care about uppercase and lowercase for the username. Is there a way I can lowercase all the letters inside both usernames before comparison

Sorry I should have just looked this up before posting

Actually I have a new problem. I have this function for signing in. I can lowercase what the player types in for the username and I have a list of all the username lowercased. How do i identify the original username from the dictionary of Sign ins. Here's the code -
def SignIn():
    global username
    signInListlower = []
    for i in signInList:
        signInListlower.append(string.lower(i))
    username = input('(Type "new" for new account) Type in username: ')

    if username == 'new':
        newUsername = input('Type your Username: ')
        if string.lower(newUsername) in signInListlower:
            print('The username %s is already taken' %newUsername)
            SignIn()
        else:
            newPassword = input('Type your Password: ')
            username = newUsername
            print('Now return to Destination and confirm')
            signInList.update({newUsername : newPassword})
            UserDefense.update({newUsername : 1})
            UserLvl.update({newUsername : 1})
            UserExp.update({newUsername : 0})
            UserWeap.update({newUsername : WoodBat})
            UserArmor.update({newUsername : WoodenArmor})
            strUserWeap.update({newUsername : 'Wood Bat'})
            strUserArmor.update({newUsername : 'Wooden Armor'})
            UserPts.update({newUsername : 0})
            UserExtraDamage.update({newUsername : 1})
            UserAgility.update({newUsername : 1})
            UserGold.update({newUsername : 50})
            UserFloor.update({newUsername : 1})
            for x in UserLvlListings:
                x.update({newUsername : False})

            username = newUsername

    else:
        if string.lower(username) in signInListlower:
            usernameNum = signInListlower.index(lower(username))
            for k in singInList:
                if string.lower(k) == string.lower(username):
                    username = k
            password = input('Please type your password: ')
            if password == signInList[username]:
                print('Now return to Destination and confirm')
            else:
                print('Invalid Password')
                time.sleep(2)
                SignIn()
        else:
            print('Invalid Username')
            time.sleep(2)
            SignIn()
If you don't understand just tell me and I'll try to explain it more thoroughly.
Reply
#2
What's an original username?
Reply
#3
I have a dictionary of usernames and passwords. The original username would be a username from the list. During the singIn function I use lowercase on the input from the player and lowercase all the usernames and put them in a list. I then see if the lowercased input is in the lowercase list of usernames. If so I proceed. The problem is - How do I find the original username from the singInList. Oh I just got an idea. Could I possibly see which number in the list it is and then find it in the dictionary by which number username it is in the list. For example if the username comes from list[1], I would go to the second username in the dictionary.
Reply
#4
If you have a dictionary with the keys being user names, you don't need to create a new list. In fact, it's incredibly inefficient. You can check key in dictionary, and that is faster than checking a list. I would have a dictionary of the lowercase usernames as keys, and the password as the value. Then you can check password = dictionary[username.lower()] to see if they entered the correct password.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
I like to use list comprehensions when going over an iterable:
usernames = ["Tom", "Brady"]
lowered_usernames = [name.lower() for name in usernames]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 761 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,766 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  What is wrong with this strings comparison minh 3 2,087 Jan-31-2020, 07:42 PM
Last Post: minh
  Finding multiple strings between the two same strings Slither 1 2,519 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,231 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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